The Option Type In Verse
Option
The option type can contain one value or can be empty.
The option type can contain one value or can be empty.
In the following example, MaybeANumber is an optional integer ?int that contains no value. A new value for MaybeANumber is then set to 42.
| | var MaybeANumber : ?int = false # unset optional value |
| --- | --- |
| | set MaybeANumber := option{42} # assigned the value 42 |
Copy full snippet

| | | MaybeANumber : ?int = option{42} # initialized as 42 | | --- | --- | | | | | | MaybeAnotherNumber : ?int = false # unset optional value | Copy full snippet | Creating an option: You can initialize an option with one of the following: * No value: Assign false to the option to mark it as unset. * Initial value: Use the keyword option followed by {}, and an expression between the {}. If the expression fails, the option will be unset and have the value false. Specify the type by adding ? before the type of value expected to be stored in the option. For example ?int. |
| --- | --- | --- | --- | --- | --- | --- | --- |
| | | if (Number := MaybeANumber?): | | --- | --- | | | Number # if MaybeANumber is not empty, then its value is stored in Number for you to use. | Copy full snippet | Accessing an element in an option: Use the query operator ? with the option, such as MaybeANumber?. Accessing the value stored in an option is a failable expression because there might not be a value in the option, and so must be used in a failure context. |
You're reading a preview
The full reference is free for BrainDeadGuild Discord members — sign in to read it all, or open the original at the source.
Sign in with your BrainDead.TV / BrainDeadGuild Discord account for full access.