record field punning

This commit is contained in:
Gabriel Scherer 2020-12-20 16:59:55 +01:00
parent d8f7ac5d24
commit 5e8e1efc50
4 changed files with 22 additions and 5 deletions

View File

@ -287,14 +287,18 @@
(record_list_expr SEMICOLON record_item_expr) : (cons $3 $1))
(record_item_expr
(longident_field EQ expr_no_semi) : (cons $1 $3))
(longident_field EQ expr_no_semi) : (cons $1 $3)
(LIDENT) : (cons (list 'Lident $1) (lid->evar $1))
)
(record_list_pattern
(record_item_pattern) : (cons $1 #nil)
(record_list_pattern SEMICOLON record_item_pattern) : (cons $3 $1))
(record_item_pattern
(longident_field EQ pattern) : (cons $1 $3))
(longident_field EQ pattern) : (cons $1 $3)
(LIDENT) : (cons (list 'Lident $1) (lid->pvar $1))
)
(comma_separated_list2_pattern
(pattern COMMA pattern) : (cons $3 (cons $1 #nil))

View File

@ -1 +1 @@
Bytecode size: 3205 bytes
Bytecode size: 3764 bytes

View File

@ -1,6 +1,6 @@
let () = print_endline "Records:"
let () = print "simple: "
(* records *)
type t = { a : int ; b : int }
let () =
@ -11,6 +11,9 @@ let () =
let u = { b = 5 ; a = 7 } in
print_int u.a; print_int u.b
let () = print_newline ()
let () = print "with: "
let () =
let u = { a = 5 ; b = 7 } in
let v = { u with a = 42 } in
@ -20,3 +23,11 @@ let () =
print_int w.a; print_int w.b
let () = print_newline ()
let () = print "record field punning: "
let () =
let u = let a, b = 1, 2 in { a; b } in
match u with
| {a; b} -> print_int a; print_int b
let () = print_newline ()

View File

@ -1,2 +1,4 @@
Records:
5 7 7 5 5 7 42 7 5 16
simple: 5 7 7 5
with: 5 7 42 7 5 16
record field punning: 1 2