Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 1.1 KB

13.-containers.md

File metadata and controls

31 lines (22 loc) · 1.1 KB

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 for MaybeANumber is then set to 42.

{% code lineNumbers="true" %}

var MaybeANumber : ?int = false # unset optional value
set MaybeANumber := option{42} # assigned the value 42

{% endcode %}

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 value false.
  • Specify the type by adding ? before the type of value expected to be stored in the option. For example ?int.

{% code lineNumbers="true" %}

MaybeANumber : ?int = option{42} # initialized as 42
 
MaybeAnotherNumber : ?int = false # unset optional value

{% endcode %}