13. Containers
Store multiple values together by using a container . Verse has a number of container types to store values in.
Option
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 forMaybeANumber
is then set to42
.
var MaybeANumber : ?int = false # unset optional value
set MaybeANumber := option{42} # assigned the value 42
Initialization
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 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 value
Last updated
Was this helpful?