-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrecord.er
35 lines (30 loc) · 851 Bytes
/
record.er
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
# Record is a feature similar to object (literal notation) in JS
# `.` means the field is public
john = {
.name = "John Smith";
.age = !27
}
print! john.name
print! john.age
assert john.name == "John Smith"
assert john.age == 27
# john.age.update! old -> old + 1
# assert john.age == 28
# Record is not Dict, so `john["name"]` is invalid
# A record whose values are all types will also behave as a type
Person! = {
.name = Str;
.age = Nat!
}
print! Person!.name
assert Person!.name == Str
# assert john in Person!
for! {.x = 1; .y = 2}.as_dict().items(), ((k, v),) =>
# k: Str, v: Int
print! k, v
# {=} means the empty record (type), which is the subtype of all records
iterate_rec! r: {=} =
for! r.as_dict().items(), ((k, v),) =>
print! k, v
iterate_rec! {a = 1; b = 2}
iterate_rec! {a = 1; b = 1.2; c = "a"}