mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-28 04:09:05 +00:00
24 lines
531 B
Python
24 lines
531 B
Python
# 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!
|