forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass6_test.ml
78 lines (56 loc) · 1.49 KB
/
class6_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
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 = fun x_init ->
object
val mutable x = x_init
method get_x = x
method move d = x <- x + d
end
class colored_point x (c : string) =
object
inherit point x
val c = c
method color = c
end;;
let colored_point_to_point cp = (cp : colored_point :> point);;
let p = new point 3 and q = new colored_point 4 "blue";;
let rec lookup_obj obj =
function
| [] -> raise Not_found
| obj' :: l ->
if (obj :> < >) = (obj' :> < >) then obj' else lookup_obj obj l ;;
class type c' = object method m : int end;;
class c : c' = object method m = 1 end
and d = object (self)
inherit c
method n = 2
method as_c = (self :> c')
end;;
class virtual c2' = object method virtual m : int end;;
class functional_point y =
object
val x = y
method get_x = x
method move d = {< x = x + d >}
end;;
let () =
let p = new functional_point 7 in
eq __LOC__
(7,10,7)
(p#get_x, (p#move 3)#get_x , p#get_x)
class bad_functional_point y =
object
val x = y
method get_x = x
method move d = new bad_functional_point (x+d)
end;;
let () =
let p = new bad_functional_point 7 in
eq __LOC__
(7,10,7)
(p#get_x, (p#move 3)#get_x , p#get_x)
let () = Mt.from_pair_suites __FILE__ !suites