13. Containers
Store multiple values together by using a container . Verse has a number of container types to store values in.
Option
The
optiontype can contain one value or can be empty.In the following example,
MaybeANumberis an optional integer?intthat contains no value, A new value forMaybeANumberis then set to42.
var MaybeANumber : ?int = false # unset optional value
set MaybeANumber := option{42} # assigned the value 42Initialization
You can initialize an option with one of the following:
No value: Assign
falseto the option to mark it as unset.Initial value: Use the keyword
optionfollowed by{}, and an expression between the{}. If the expression fails, the option will be unset and have the valuefalse.Specify the type by adding
?before the type of value expected to be stored in the option. For example?int.
MaybeANumber : ?int = option{42} # initialized as 42
MaybeAnotherNumber : ?int = false # unset optional valueLast updated
Was this helpful?