Unions and Enums
Use unions for explicit states, and enums for simple named constants.
Variants
A union lists the possible shapes a value can have.
variant.brp
union Download:
Queued
Running(Int)Variants With Data
A variant can carry data, such as Running(Int).
payload.brp
status: Download = Running(40)Enums
An enum is for variants with no payload.
enum.brp
enum Direction:
North
SouthExplicit States
Modeling states as variants prevents impossible combinations of fields.
states.brp
state: Download = Failed("network")Example
download.brp
union Download:
Queued
Running(Int)
Done(String)
Failed(String)
pure func describe(download: Download) -> String:
match download:
Queued: "queued"
Running(percent): "running " + percent.to_string()
Done(path): "done: " + path
Failed(msg): "failed: " + msg
func main(args: List[String]) -> Void:
print(describe(Running(40)))
Try It
terminal
blorp run download.brp