File: includestruct.ml

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

(* Test for "include <module-expr>" inside structures *)

module A =
  struct
    type t = int
    let x = (1 : t)
    let y = (2 : t)
    let f (z : t) = (x + z : t)
  end

module B =
  struct
    include A
    type u = t * t
    let p = ((x, y) : u)
    let g ((x, y) : u) = ((f x, f y) : u)
  end

let _ =
  let print_pair (x,y) =
    print_int x; print_string ", "; print_int y; print_newline() in
  print_pair B.p;
  print_pair (B.g B.p);
  print_pair (B.g (123, 456))

module H =
  struct
    include A
    let f (z : t) = (x - 1 : t)
  end

let _ =
  print_int (H.f H.x); print_newline()

module C =
  struct
    include (A : sig type t val f : t -> int val x : t end)
    let z = f x
  end

let _ =
  print_int C.z; print_newline();
  print_int (C.f C.x); print_newline()

(* Toplevel inclusion *)

include A

let _ =
  print_int x; print_newline();
  print_int (f y); print_newline()

(* With a functor *)

module F(X: sig end) =
  struct
    let _ = print_string "F is called"; print_newline()
    type t = A | B of int
    let print_t = function A -> print_string "A"
                         | B x -> print_int x
  end

module D =
  struct
    include F(struct end)
    let test() = print_t A; print_newline(); print_t (B 42); print_newline()
  end

let _ =
  D.test();
  D.print_t D.A; print_newline(); D.print_t (D.B 42); print_newline()

(* Exceptions and classes *)

module E =
  struct
    exception Exn of string
    class c = object method m = 1 end
  end

module G =
  struct
    include E
    let _ =
      begin try raise (Exn "foo") with Exn s -> print_string s end;
      print_int ((new c)#m); print_newline()
  end

let _ =
  begin try raise (G.Exn "foo") with G.Exn s -> print_string s end;
  print_int ((new G.c)#m); print_newline()



include (struct
  let a = 10
  module X = struct let x = 1 let z = 42 let y = 2 end
  exception XXX
end : sig
  module X : sig val y: int val x: int end
  exception XXX
  val a: int
end)

let () =
  Printf.printf "%i / %i / %i \n%!" X.x X.y a;
  Printf.printf "%s\n%!" (Printexc.to_string XXX)