1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
# Object Oriented Programming
In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details.
A simple class:
```yuescript
class Inventory
new: =>
@items = {}
add_item: (name) =>
if @items[name]
@items[name] += 1
else
@items[name] = 1
```
<YueDisplay>
```yue
class Inventory
new: =>
@items = {}
add_item: (name) =>
if @items[name]
@items[name] += 1
else
@items[name] = 1
```
</YueDisplay>
A class is declared with a class statement followed by a table-like declaration where all of the methods and properties are listed.
The new property is special in that it will become the constructor.
Notice how all the methods in the class use the fat arrow function syntax. When calling methods on a instance, the instance itself is sent in as the first argument. The fat arrow handles the creation of a self argument.
The @ prefix on a variable name is shorthand for self.. @items becomes self.items.
Creating an instance of the class is done by calling the name of the class as a function.
```yuescript
inv = Inventory!
inv\add_item "t-shirt"
inv\add_item "pants"
```
<YueDisplay>
```yue
inv = Inventory!
inv\add_item "t-shirt"
inv\add_item "pants"
```
</YueDisplay>
Because the instance of the class needs to be sent to the methods when they are called, the \ operator is used.
All properties of a class are shared among the instances. This is fine for functions, but for other types of objects, undesired results may occur.
Consider the example below, the clothes property is shared amongst all instances, so modifications to it in one instance will show up in another:
```yuescript
class Person
clothes: []
give_item: (name) =>
table.insert @clothes, name
a = Person!
b = Person!
a\give_item "pants"
b\give_item "shirt"
-- will print both pants and shirt
print item for item in *a.clothes
```
<YueDisplay>
```yue
class Person
clothes: []
give_item: (name) =>
table.insert @clothes, name
a = Person!
b = Person!
a\give_item "pants"
b\give_item "shirt"
-- will print both pants and shirt
print item for item in *a.clothes
```
</YueDisplay>
The proper way to avoid this problem is to create the mutable state of the object in the constructor:
```yuescript
class Person
new: =>
@clothes = []
```
<YueDisplay>
```yue
class Person
new: =>
@clothes = []
```
</YueDisplay>
## Inheritance
The extends keyword can be used in a class declaration to inherit the properties and methods from another class.
```yuescript
class BackPack extends Inventory
size: 10
add_item: (name) =>
if #@items > size then error "backpack is full"
super name
```
<YueDisplay>
```yue
class BackPack extends Inventory
size: 10
add_item: (name) =>
if #@items > size then error "backpack is full"
super name
```
</YueDisplay>
Here we extend our Inventory class, and limit the amount of items it can carry.
In this example, we don't define a constructor on the subclass, so the parent class' constructor is called when we make a new instance. If we did define a constructor then we can use the super method to call the parent constructor.
Whenever a class inherits from another, it sends a message to the parent class by calling the method __inherited on the parent class if it exists. The function receives two arguments, the class that is being inherited and the child class.
```yuescript
class Shelf
@__inherited: (child) =>
print @__name, "was inherited by", child.__name
-- will print: Shelf was inherited by Cupboard
class Cupboard extends Shelf
```
<YueDisplay>
```yue
class Shelf
@__inherited: (child) =>
print @__name, "was inherited by", child.__name
-- will print: Shelf was inherited by Cupboard
class Cupboard extends Shelf
```
</YueDisplay>
## Super
**super** is a special keyword that can be used in two different ways: It can be treated as an object, or it can be called like a function. It only has special functionality when inside a class.
When called as a function, it will call the function of the same name in the parent class. The current self will automatically be passed as the first argument. (As seen in the inheritance example above)
When super is used as a normal value, it is a reference to the parent class object.
It can be accessed like any of object in order to retrieve values in the parent class that might have been shadowed by the child class.
When the \ calling operator is used with super, self is inserted as the first argument instead of the value of super itself. When using . to retrieve a function, the raw function is returned.
A few examples of using super in different ways:
```yuescript
class MyClass extends ParentClass
a_method: =>
-- the following have the same effect:
super "hello", "world"
super\a_method "hello", "world"
super.a_method self, "hello", "world"
-- super as a value is equal to the parent class:
assert super == ParentClass
```
<YueDisplay>
```yue
class MyClass extends ParentClass
a_method: =>
-- the following have the same effect:
super "hello", "world"
super\a_method "hello", "world"
super.a_method self, "hello", "world"
-- super as a value is equal to the parent class:
assert super == ParentClass
```
</YueDisplay>
**super** can also be used on left side of a Function Stub. The only major difference is that instead of the resulting function being bound to the value of super, it is bound to self.
## Types
Every instance of a class carries its type with it. This is stored in the special __class property. This property holds the class object. The class object is what we call to build a new instance. We can also index the class object to retrieve class methods and properties.
```yuescript
b = BackPack!
assert b.__class == BackPack
print BackPack.size -- prints 10
```
<YueDisplay>
```yue
b = BackPack!
assert b.__class == BackPack
print BackPack.size -- prints 10
```
</YueDisplay>
## Class Objects
The class object is what we create when we use a class statement. The class object is stored in a variable of the same name of the class.
The class object can be called like a function in order to create new instances. That's how we created instances of classes in the examples above.
A class is made up of two tables. The class table itself, and the base table. The base is used as the metatable for all the instances. All properties listed in the class declaration are placed in the base.
The class object's metatable reads properties from the base if they don't exist in the class object. This means we can access functions and properties directly from the class.
It is important to note that assigning to the class object does not assign into the base, so it's not a valid way to add new methods to instances. Instead the base must explicitly be changed. See the __base field below.
The class object has a couple special properties:
The name of the class as when it was declared is stored as a string in the __name field of the class object.
```yuescript
print BackPack.__name -- prints Backpack
```
<YueDisplay>
```yue
print BackPack.__name -- prints Backpack
```
</YueDisplay>
The base object is stored in __base. We can modify this table to add functionality to instances that have already been created and ones that are yet to be created.
If the class extends from anything, the parent class object is stored in __parent.
## Class Variables
We can create variables directly in the class object instead of in the base by using @ in the front of the property name in a class declaration.
```yuescript
class Things
@some_func: => print "Hello from", @__name
Things\some_func!
-- class variables not visible in instances
assert Things().some_func == nil
```
<YueDisplay>
```yue
class Things
@some_func: => print "Hello from", @__name
Things\some_func!
-- class variables not visible in instances
assert Things().some_func == nil
```
</YueDisplay>
In expressions, we can use @@ to access a value that is stored in the __class of self. Thus, @@hello is shorthand for self.__class.hello.
```yuescript
class Counter
@count: 0
new: =>
@@count += 1
Counter!
Counter!
print Counter.count -- prints 2
```
<YueDisplay>
```yue
class Counter
@count: 0
new: =>
@@count += 1
Counter!
Counter!
print Counter.count -- prints 2
```
</YueDisplay>
The calling semantics of @@ are similar to @. Calling a @@ name will pass the class in as the first argument using Lua's colon syntax.
```yuescript
@@hello 1,2,3,4
```
<YueDisplay>
```yue
@@hello 1,2,3,4
```
</YueDisplay>
## Class Declaration Statements
In the body of a class declaration, we can have normal expressions in addition to key/value pairs. In this context, self is equal to the class object.
Here is an alternative way to create a class variable compared to what's described above:
```yuescript
class Things
@class_var = "hello world"
```
<YueDisplay>
```yue
class Things
@class_var = "hello world"
```
</YueDisplay>
These expressions are executed after all the properties have been added to the base.
All variables declared in the body of the class are local to the classes properties. This is convenient for placing private values or helper functions that only the class methods can access:
```yuescript
class MoreThings
secret = 123
log = (msg) -> print "LOG:", msg
some_method: =>
log "hello world: " .. secret
```
<YueDisplay>
```yue
class MoreThings
secret = 123
log = (msg) -> print "LOG:", msg
some_method: =>
log "hello world: " .. secret
```
</YueDisplay>
## @ and @@ Values
When @ and @@ are prefixed in front of a name they represent, respectively, that name accessed in self and self.__class.
If they are used all by themselves, they are aliases for self and self.__class.
```yuescript
assert @ == self
assert @@ == self.__class
```
<YueDisplay>
```yue
assert @ == self
assert @@ == self.__class
```
</YueDisplay>
For example, a quick way to create a new instance of the same class from an instance method using @@:
```yuescript
some_instance_method = (...) => @@ ...
```
<YueDisplay>
```yue
some_instance_method = (...) => @@ ...
```
</YueDisplay>
## Constructor Property Promotion
To reduce the boilerplate code for definition of simple value objects. You can write a simple class like:
```yuescript
class Something
new: (@foo, @bar, @@biz, @@baz) =>
-- Which is short for
class Something
new: (foo, bar, biz, baz) =>
@foo = foo
@bar = bar
@@biz = biz
@@baz = baz
```
<YueDisplay>
```yue
class Something
new: (@foo, @bar, @@biz, @@baz) =>
-- Which is short for
class Something
new: (foo, bar, biz, baz) =>
@foo = foo
@bar = bar
@@biz = biz
@@baz = baz
```
</YueDisplay>
You can also use this syntax for a common function to initialize a object's fields.
```yuescript
new = (@fieldA, @fieldB) => @
obj = new {}, 123, "abc"
print obj
```
<YueDisplay>
```yue
new = (@fieldA, @fieldB) => @
obj = new {}, 123, "abc"
print obj
```
</YueDisplay>
## Class Expressions
The class syntax can also be used as an expression which can be assigned to a variable or explicitly returned.
```yuescript
x = class Bucket
drops: 0
add_drop: => @drops += 1
```
<YueDisplay>
```yue
x = class Bucket
drops: 0
add_drop: => @drops += 1
```
</YueDisplay>
## Anonymous classes
The name can be left out when declaring a class. The __name attribute will be nil, unless the class expression is in an assignment. The name on the left hand side of the assignment is used instead of nil.
```yuescript
BigBucket = class extends Bucket
add_drop: => @drops += 10
assert Bucket.__name == "BigBucket"
```
<YueDisplay>
```yue
BigBucket = class extends Bucket
add_drop: => @drops += 10
assert Bucket.__name == "BigBucket"
```
</YueDisplay>
You can even leave off the body, meaning you can write a blank anonymous class like this:
```yuescript
x = class
```
<YueDisplay>
```yue
x = class
```
</YueDisplay>
## Class Mixing
You can do mixing with keyword `using` to copy functions from either a plain table or a predefined class object into your new class. When doing mixing with a plain table, you can override the class indexing function (metamethod `__index`) to your customized implementation. When doing mixing with an existing class object, the class object's metamethods won't be copied.
```yuescript
MyIndex = __index: var: 1
class X using MyIndex
func: =>
print 123
x = X!
print x.var
class Y using X
y = Y!
y\func!
assert y.__class.__parent ~= X -- X is not parent of Y
```
<YueDisplay>
```yue
MyIndex = __index: var: 1
class X using MyIndex
func: =>
print 123
x = X!
print x.var
class Y using X
y = Y!
y\func!
assert y.__class.__parent ~= X -- X is not parent of Y
```
</YueDisplay>
|