Records, enums and match
Records: data with named fields
A record gives your data a shape. Declare it once at the top of the file, then create values with named fields.
record Player:
name: text
hp: num
fn hit(p: Player, damage: num) -> Player:
p.hp -= damage
return p
fn main() needs print:
hero = Player(name: "Ada", hp: 30)
hit(hero, 12)
print(hero)
print(hero.name, hero.hp)
print(hero == Player(name: "Ada", hp: 18))
Fields are read and changed with a dot. Records compare by content. Printing one shows Player(name: "Ada", hp: 18).
Cresset checks records before the program runs. Missing fields, unknown fields, fields of the wrong type and typos are all caught, with suggestions:
record Player:
name: text
hp: num
fn main() needs print:
hero = Player(name: "Ada", hp: "thirty")
print(hero)
error: Player.hp is a number, but this is text
Methods: functions that belong to a record
Put functions inside a record, after its fields. The first parameter is always self, the record the method was called on, and you call it with a dot:
record Player:
name: text
hp: num
fn heal(self, amount: num) -> Player:
return Player(name: self.name, hp: self.hp + amount)
fn alive(self) -> bool:
return self.hp > 0
fn main() needs print:
hero = Player(name: "Ada", hp: 3)
print(hero.heal(4).hp, hero.alive())
A method is an ordinary function with a nicer way to call it, so everything you already know still holds:
- Types are checked. Wrong argument types, a wrong number of arguments, or a misspelled method name are caught before the program runs, with a "did you mean".
- Permissions are enforced. A method declares what it touches with
needs, like any function, and so must anyone who calls it. - Methods don't change the record.
hero.heal(4)gives back a newPlayer;herokeeps its old value. Return a new record when you want a changed one. - Packages share them. A record from a package arrives with its methods. Names that start with
_stay private to the package.
record Player:
name: text
hp: num
fn heal(self, amount: num) -> Player:
return Player(name: self.name, hp: self.hp + amount)
fn main() needs print:
hero = Player(name: "Ada", hp: 3)
print(hero.heal("a lot"))
error: .heal() expects 'amount' to be a number, but this is text
Two rules keep methods simple. Fields come first and methods after, and a method can't have the same name as a field. Because a call like x.say() can't always be traced to one record, Cresset is strict about permissions here: calling any method called say counts as using what any say method in your program or its packages needs.
Enums: one of several choices
An enum is a value that is exactly one of a fixed set of named choices. A choice can carry fields.
enum Status:
Alive
Hurt(hp: num)
Dead
fn main() needs print:
a = Status.Alive
b = Status.Hurt(hp: 3)
print(a, b, a == Status.Alive)
Choices without fields are written plainly (Status.Alive). Choices with fields are created with named fields (Status.Hurt(hp: 3)).
match: look inside an enum
match picks the case that fits and gives names to the fields:
enum Status:
Alive
Hurt(hp: num)
Dead
fn describe(s: Status) -> text:
match s:
Alive:
return "all good"
Hurt(hp):
return "hurt, {hp} hp left"
Dead:
return "down"
fn main() needs print:
print(describe(Status.Hurt(hp: 6)))
print(describe(Status.Dead))
In Hurt(hp), the name hp gets the value of the field. Write _: as a final case to handle everything else.
Forgetting a case is an error
This is the best reason to use an enum. If a match doesn't cover every choice and has no _:, Cresset refuses to run the program:
enum Status:
Alive
Hurt(hp: num)
Dead
fn describe(s: Status) -> text:
match s:
Alive:
return "all good"
Hurt(hp):
return "hurt"
fn main() needs print:
print(describe(Status.Dead))
error: this match does not cover: Dead --> example.crs:7 help: add a case for it, or _: to handle anything else
Add a new choice to an enum later and every match that forgot it is flagged immediately. Cresset also catches unknown choices, the wrong number of fields in a case, and a case written twice.
Notes
- Records and enums are declared at the top level of a file.
- They work with record and replay.
- A package can share its records and enums. Use them as
package.Name.
Next
How failures are handled: Errors and contracts.