-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcandle_records.ml
54 lines (44 loc) · 1.74 KB
/
candle_records.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
(* Record syntax in Candle. *)
(* Records are declared as part of datatypes. It is possible to mix classic
* datatype constructors and record constructors freely.
*)
type thing =
| Aa of int
| Bb of { integer: int; boolean: bool; a_string: string }
;;
(* Records support pattern matching. It is possible to do partial matches. *)
let m1 t =
match t with
| Aa n -> n
| Bb {integer} -> integer;;
(* Pattern matching on records works in the outermost pattern. Nested record
* patterns are not supported.
*)
let Bb { a_string } = Bb { a_string = "5"; boolean = false; integer = 1 };;
(* Records are constructed with this syntax: *)
let x = Bb { a_string = "1"; boolean = false; integer = 2 };;
let y = Aa 5;;
(* Records can be updated using 'with'. This updates the record 'x': *)
let z = Bb { x with boolean = true; integer = 3; };;
(* Record projection is done with dots, using the name of the constructor and
* the name of the field: *)
let s = z.Bb.a_string;;
print (s ^ "\n");;
print (Int.toString (m1 x) ^ " " ^ Int.toString (m1 y) ^ "\n");;
print (a_string ^ "\n");;
module Mod = struct
type blob = Cc of { cc: bool };;
let x = Cc { cc = true };;
end;;
(* It's possible to use records with qualified constructors:
* - Mod.x is to access the value x,
* - Mod.Cc is for the record constructor in Cc in Mod, and
* - cc is the field in the record Cc: *)
print "cc=";;
print (if Mod.x.Mod.Cc.cc then "true\n" else "false\n");;
(* Limitations:
* - It is not possible to name the matched fields in record pattern matches
* - Record constructors are present in all record-related syntax. This is
* because records are implemented as a syntax extension in the Candle parser,
* and the parser does not know what names belong to what records.
*)