aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/type/object.lux
blob: 7ca6017920b8daa5f4ec50ff6e1a3bb973bcf767 (plain)
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
(.module:
  lux
  (lux (data (coll [list]))
       (type object)))

## No parameters
(interface: Counter
  (inc [] @)
  (read [] Nat))

(class: NatC Counter
  Nat

  (def: inc
    (update@Counter n/inc))
  
  (def: read
    get@Counter))

(interface: Resettable-Counter
  #super Counter
  (reset [] @))

(class: NatRC Resettable-Counter
  #super NatC
  Unit

  (def: reset
    (set@Counter +0)))

## With parameters
(interface: (Collection a)
  (add [a] @)
  (size [] Nat))

(class: (ListC a) (Collection a)
  (List a)

  (def: (add elem)
    (update@Collection (|>> (#.Cons elem))))

  (def: size
    (|>> get@Collection list.size)))

(interface: (Iterable a)
  #super (Collection a)
  (enumerate [] (List a)))

(class: (ListI a) (Iterable a)
  #super (ListC a)
  Unit

  (def: enumerate
    get@Collection))

## Polymorphism
(def: (poly0 counter)
  (-> Counter Nat)
  (read counter))

(def: poly0-0 Nat (poly0 (new@NatC +0)))
(def: poly0-1 Nat (poly0 (new@NatRC +0 [])))

(def: (poly1 counter)
  (-> Resettable-Counter Nat)
  (n/+ (read counter)
       (read (reset counter))))

(def: poly1-0 Nat (poly1 (new@NatRC +0 [])))

(def: (poly2 counter)
  (-> NatC Nat)
  (read counter))

(def: poly2-0 Nat (poly2 (new@NatC +0)))
(def: poly2-1 Nat (poly2 (new@NatRC +0 [])))

(def: (poly3 counter)
  (-> NatRC Nat)
  (n/+ (read counter)
       (read (reset counter))))

(def: poly3-0 Nat (poly3 (new@NatRC +0 [])))