Optionals

Types can be decorated with the optional modifier indicating the variable may or may not be set. The core syntax has been designed around optionals to provide a simple and consistent use.

Refer to (understanding conditionals)[/features/conditionals.html] for a thorough explanation.

var a : optional integer = maybe_get_number()

//copies a's value, raises an error if a has not been set
var b : integer = a 

//copies a's value, or uses 8 if as has no value
var c = a | 8 

The conditional operator creates optionals, allowing assignment or construction of a traditional ternary operator.

//a is an 'optional integer' either set to 5, or not set
var a = x < y ? 5 

//b is set to either 8 or 9
var b = y < z ? 8 | 9 

To keep with the goals of Leaf, this syntax is also used to do conditional code execution. The do statement is understood as taking an optional code block, and executing it if set, or doing nothing if not set.

//if statement
do x < y ? {
    //code
}

//if-else statement
do x < y ? {
    //then
} | {
    //else
}