File: oo.ls

package info (click to toggle)
node-livescript 1.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 672 kB
  • sloc: makefile: 45
file content (469 lines) | stat: -rw-r--r-- 7,711 bytes parent folder | download | duplicates (4)
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
# Test classes with a four-level inheritance chain.
class Base
  func: (string) ->
    "zero/#{string}"

  @static = (string) ->
    "static/#{string}"

class FirstChild extends Base
  func: -> super('one/').concat it

SecondChild = class extends FirstChild
  func: -> (super).call(this, 'two/') + it

thirdCtor = -> @array = [1, 2, 3]

class ThirdChild extends SecondChild
  -> thirdCtor ...

  name = -> 'func'

  # `super` from a dynamically named method and an inner function.
  (name!): ->
    let it
      super('three/') + it

eq (new ThirdChild).func('four'), 'zero/one/two/three/four'
eq Base.static('word'), 'static/word'

eq (new ThirdChild).array.join(' '), '1 2 3'


# `extends` operator
First  = ->
Second = ->

Second extends First extends Base

ok new Second(2).func(), 'zero/2'


# `@` referring to the current instance, and not being coerced into a call.
ok (new class I then amI: -> @ instanceof I).amI()


# `super` calls in constructors of classes that are defined as object properties.
Bee       = class then (@name, ...@attributes) ->
Bee.Honey = class extends Bee then -> super ...

bee = new Bee.Honey 'Maya' \adventurous \flighty
eq "#{bee.name} is #{ bee.attributes.join ' and ' }."
 , 'Maya is adventurous and flighty.'


# Test calling super and passing along all arguments.
class Parent
  method: (...args) -> @args = args

class Child extends Parent
  method: -> super ...

c = new Child
c.method 1, 2, 3, 4
eq c.args.join(' '), '1 2 3 4'


# `superclass` injection
let superclass = Object
  eq super, Object


# Class with JS-keyword properties.
class Class
  class: 'class'
  name: -> @class

instance = new Class
eq instance.class, 'class'
eq instance.name(), 'class'


# Static method as a bound function.
class Dog
  (@name) ->
  @static = ~> new this 'Dog'

eq {func: Dog.static}.func().name, 'Dog'


# A bound function in a bound method.
class Mini
  ->
    @generate = ~>
      for let i from 1 to 3
        ~> @num * i
  num: 10

eq [func() for func in new Mini().generate()] + '', '10,20,30'


# Test classes wrapped in decorators.
func = (klass) ->
  klass::prop = 'value'
  klass

func class Test
  prop2: 'value2'

eq (new Test).prop , 'value'
eq (new Test).prop2, 'value2'


# Test anonymous classes.
obj =
  klass: class
    method: -> 'value'

instance = new obj.klass
eq instance.method(), 'value'


# Implicit objects as static properties.
class Static
  @static =
    one: 1
    two: 2

eq Static.static.one, 1
eq Static.static.two, 2


# Namespaced,
Namespace = {}
Class     = null

# but undeclared.
Namespace.Class = class
eq Namespace.Class.displayName, 'Class'
eq Class, null

# and declared.
class Namespace.Class
eq Class, Namespace.Class


class BoundCtor extends (-> {@attr})
  (@attr, ret) ~>
    return this if ret
    eq super(...).attr, @attr
    @method = ~> this

class BoundChild extends BoundCtor
  ~>
    super ...
    # Auto-`return this` even with backcall.
    <- Object

for C in [BoundCtor, BoundChild]
  bc = C 'attr'
  eq bc.attr, 'attr'
  eq [bc.method][0](), bc
eq BoundCtor(8, true).attr, 8


class Importer
  -> this import it
  method1: this
  method2: Array

class NewSuper extends Importer
  -> eq new super({ok}).ok, ok
  method1: -> new super it

ns = new NewSuper
eq ns.method1({1})[1], 1
eq ns.method2(2).length, 2


# [coffee#1009](https://github.com/jashkenas/coffee-script/issues/1009)
# Class names are "$"-prefixed when reserved.
new
  class @in
  class @eval
  ok $in?
  ok $eval?


class OddSuper
  super: me = -> this
  $uper: me
  1234 : me
  5.67 : me
  '""' : me

class OddSuperSub extends OddSuper
  super: -> super()
  $uper: -> super()
  1234 : -> do super
  5.67 : -> super 8
  '""' : -> super ...@@

oss = new OddSuperSub
eq oss.super(), oss
eq oss.$uper(), oss
eq oss[1234](), oss
eq oss[5.67](), oss
eq oss['""'](), oss


eq \declared (class declared)displayName
ok declared?

eq \named  (new -> return @named = class)displayName
ok not named? 'should not leak to global when undeclared'


# `super` with nested classes
class Sup
  class @Sub extends this
    eq super, Sup
  method: function
    class extends @.@@
      eq super, Sup
      method: ->
        eq super, method

ok new Sup.Sub instanceof Sup
(new (new Sup!method!))method!


# `prototype`/`constructor`/`superclass` under class body
new class extends Object
  eq ::, prototype
  eq ::, @::
  eq @@, @
  ok super is superclass is Object
  ->
    eq ::, @.@@::
    eq @@, @.@@


# `super::method`
class
  method: -> true
  class C extends this
    method: -> false
    test  : -> super::method!
  ok new C!test!


# `extended` hook
class NameEater
  @subnames = []
  @extended = ->
    eq it.superclass, this
    @subnames.push it.displayName

class A extends NameEater
  (class B) extends NameEater

eq 'A,B' ''+NameEater.subnames


# Line folding around `extends`
class   Inject
extends Object
  class Reject extends
        Object
    ok true


#### `implements`
Mover =
  x: 0, y: 0
  moveTo: (@x, @y) -> this
  moveBy: (dx, dy) -> @x += dx; @y += dy; this

Magnitude =
  lt  : -> ...
  gt  : -> it.lt this
  lte : -> @lt it or @eq it
  gte : -> @gt it or @eq it
  eq  : -> not @neq it
  neq : -> @lt it or @gt it

Measurable =
  lt: -> @measure! < it.measure!

class Point implements Mover
  isAt: -> @x is it.x and @y is it.y

class Rect extends Point implements Magnitude, Measurable
  (@w, @h) ->
  measure: -> @w * @h

r0 = new Rect 2 3
r1 = new Rect 5 7
r0.moveTo 1, 1
r1.moveBy 1, 1
ok r0.isAt r1
ok r0.neq  r1
ok r1.gte  r0
ok r0.eq new Rect 1 6

ok class extends Function '' implements
                               {}
  class
  implements {}
    void


### Clone
bird     = {+wing, fly: -> @wing}
wingless = {-wing}

duck    = ^^bird
dodo    = ^^bird <<< {...wingless, +extinct }
donaldo = ^^duck <<< {...wingless, +domestic}

ok bird.fly()
ok duck.fly()
ok not donaldo.fly()

ok ^^new Number instanceof Number
eq (^^new Number)@@, Number

# Inherit constructors
class A
  -> @x = 5

class B extends A
  getX: -> @x

eq 5 (new B)getX!

# No body
class C extends B

eq 5 (new C)getX!

class D
  extends C
eq 5 (new D)getX!

# Bound methods are bound to instance not class, 
# however bound static funcs are still bound to class
class G
  ->
    @x = 5
    @y = 6
  getX: -> @x
  getY: ~> @y
  @x = \staticX
  @y = \staticY
  @getStatX = -> @x
  @getStatY = ~> @y

g = new G
obj = x: 0, y: 0
obj{getX, getY} = g
obj{getStatY, getStatX} = G

eq 0 obj.getStatX!
eq \staticY obj.getStatY!

eq 0 obj.getX!
eq 6 obj.getY!

class H extends G

h = new H
obj = x: 0, y: 0
obj{getX, getY} = h

eq 0 obj.getX!
eq 6 obj.getY!

# Inherit static
class A
  @stat = -> 2

class B extends A

eq 2 B.stat!

class C extends B
  @stat = -> 2 + super!

eq 4 C.stat!


# Super outside of class defintions
class A
  meth: -> it + 5
  @stat = -> it + 5

a = new A
eq 10 a.meth 5
eq 10 A.stat 5

class B extends A

b = new B
eq 10 b.meth 5
eq 10 B.stat 5

B::meth = -> super it + 2
B.stat = -> super it + 2

eq 12 b.meth 5
eq 12 B.stat 5

B::meth = (x) ->
  func = ->
    super x + 2
  func!
B.stat = (x) ->
  func = ->
    super x + 2
  func!
eq 12 b.meth 5
eq 12 B.stat 5


# can define external constructor
f = -> @y = @x + it
class A
  x: 8
  constructor$$: f

eq 13 (new A 5).y


# using @@ to reference static props
class A
  @static = 2

  meth: ->
    @@static * 2

eq 4 (new A).meth!


# @@@ references @.@@ rather than the nonsensical @@.@
let @ = constructor: 10
  eq 10 @@@

# Executable class bodies and let
class A
  a: 2
  -> b: 2

eq 2 (new A).a
eq void (new A).b

# complex extends and auto super
x =
  A: class
    -> @y = \ha

class B extends x.A
  y: \no

eq \ha (new B).y

class C extends NON-EXISTANT ? (->)
  y: 4

eq 4 (new C).y