File: toplevel_lets.ml

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (71 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download | duplicates (2)
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
(* TEST *)

(* Evaluation order for class expressions *)

(* Everything in a class definition is evaluated at object creation time,
   except for any toplevel let-bindings which are lifted away and
   evaluated at class creation time. *)
let () = print_endline "M1:"
module M1 = struct
  let () = print_endline "Before class"
  class c =
    let () = print_endline "Class init" in
    object end
  let () = print_endline "After class"
  let o1 = new c
  let o2 = new c
end

(* PR 13179 *)
let () = print_endline "M2:"
module M2 = struct
  let () = print_endline "Before class"
  class c =
    let open Unit in
    let () = print_endline "Class init" in
    object end
  let () = print_endline "After class"
  let o1 = new c
  let o2 = new c
end

(* Applications: argument evaluated later *)
let () = print_endline "M3:"
module M3 = struct
  class with_param p = object end
  let () = print_endline "Before class"
  class c =
    let () = print_endline "Class init" in
    with_param (print_endline "Class param")
  let () = print_endline "After class"
  let o1 = new c
  let o2 = new c
end

(* Nested bindings are not toplevel *)
(* Not testing for side effects in arguments, as bytecode and native compilers
   produce different evaluation orders *)
let () = print_endline "M4:"
module M4 = struct
  class with_param p = object end
  let () = print_endline "Before class"
  class c =
    (let () = print_endline "Class init" in
     with_param)
      ()
  let () = print_endline "After class"
  let o1 = new c
  let o2 = new c
end

(* Constraints prevent lifting *)
let () = print_endline "M5:"
module M5 = struct
  let () = print_endline "Before class"
  class c =
    (let () = print_endline "Class init" in
     object end : object end)
  let () = print_endline "After class"
  let o1 = new c
  let o2 = new c
end