forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass3_test.ml
197 lines (148 loc) · 3.8 KB
/
class3_test.ml
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
let suites : Mt.pair_suites ref = ref []
let test_id = ref 0
let eq loc x y =
incr test_id ;
suites :=
(loc ^" id " ^ (string_of_int !test_id), (fun _ -> Mt.Eq(x,y))) :: !suites
class point x_init =
object
val mutable x = x_init
method get_x = x
method move d = x <- x + d
end;;
let p = new point 7
let () = eq __LOC__ (p # get_x) 7
class adjusted_point x_init =
let origin = (x_init / 10) * 10 in
object
val mutable x = origin
method get_x = x
method get_offset = x - origin
method move d = x <- x + d
end
let () = eq __LOC__ ((new adjusted_point 31) # get_x) 30
(* ;; Js.log @@ (new adjusted_point 31) # get_x (\* 30 *\) *)
class adjusted_point2 x_init = point ((x_init / 10) * 10)
let () = eq __LOC__ ( (new adjusted_point2 31) #get_x) 30
(* ;; Js.log @@ (new adjusted_point2 31) # get_x (\* 30 *\) *)
class printable_point x_init =
object (s)
val mutable x = x_init
method get_x = x
method move d = x <- x + d
method print = s#get_x
end;;
let p = new printable_point 7
let ()= eq __LOC__ (p#print) 7
(* ;; Js.log p#print (\* 7 *\) *)
let my_int =
let ints = ref [] in
object (self)
method n = 1
method register = ints := self :: !ints
method len = List.length !ints
end;;
let () =
my_int # register ;
my_int # register ;
Js.log @@ my_int # len (* 2 *)
let v = [|0; 3 |]
class printable_point2 x_init =
let origin = (x_init / 10) * 10 in
object (self)
val mutable x = origin
method get_x = x
method move d = x <- x + d
method print = print_int self#get_x
initializer print_endline ("initializing" ^ __LOC__); v.(0) <- x
end;;
let p = new printable_point2 31
let () = eq __LOC__ v [|30;3|]
(* ;; Js.log v (\* [30, 3]*\) *)
(* virtual methods *)
class virtual abstract_point x_init =
object (self)
method virtual get_x : int
method get_offset = self#get_x - x_init
method virtual move : int -> unit
end;;
class vpoint x_init =
object
inherit abstract_point x_init
val mutable x = x_init
method get_x = x
method move d = x <- x + d
end;;
let v =
let h = new vpoint 3 in
h # move 32;
h # get_offset
let () = eq __LOC__ v 32
(* ;;Js.log v (\* 32 *\) *)
class virtual abstract_point2 =
object
val mutable virtual x : int
method move d = x <- x + d
end
class point2 x_init =
object
inherit abstract_point2
val mutable x = x_init
method get_offset = x - x_init
end
let vv =
let h = new point2 3 in
h # move 32;
h # get_offset
let () = eq __LOC__ vv 32
(* ;; Js.log vv (\* 32 *\) *)
class restricted_point x_init =
object (self)
val mutable x = x_init
method get_x = x
method private move d = x <- x + d
method bump = self#move 1
end;;
let p = new restricted_point 0;;
let h = p # bump ; p#get_x
let () = eq __LOC__ h 1
(* ;; Js.log h (\* 1 *\) *)
class point_again x =
object (self)
inherit restricted_point x
method virtual move : _
end;;
let hh =
let p = new point_again 3 in
p # move 3 ;
p # bump;
p# bump ;
p#get_x
let () = eq __LOC__ hh 8
(* ;; Js.log hh (\* 8 *\) *)
class point_again2 x =
object (self : < move : _; ..> )
inherit restricted_point x
end;;
let hhh =
let p = new point_again2 3 in
p # move 30 ;
p # bump;
p# bump ;
p#get_x
let () = eq __LOC__ hhh 35
(* ;; Js.log hhh (\* 35 *\) *)
class point_again3 x =
object
inherit restricted_point x as super
method move = super#move
end;;
let hhhh =
let p = new point_again3 3 in
p # move 300 ;
p # bump;
p# bump ;
p#get_x
let () = eq __LOC__ hhhh 305
(* ;; Js.log hhhh (\* 305 *\) *)
;; Mt.from_pair_suites __FILE__ !suites