Conditionals

Minilang has several constructs for conditional evaluation of code.

Important

All conditional evaluation constructs treat only nil as the false-like or negative value, and all other values as true-like or positive. Boolean values (true and false) are not nil and hence treated as true-like or positive.

And / or expressions

These expressions check their left argument for nil and only evaluate their right argument if necessary.

They operate as follows:

Expression

Result of X

Y evaluated

Result

X and Y

nil

No

X (nil)

X and Y

Not nil

Yes

Y

X or Y

nil

Yes

Y

X or Y

Not nil

No

X

If expressions

An if-expression evaluates its condition expressions and evaluates the then-block if the condition value is not nil. Otherwise it evaluates the else-block is present, or nil otherwise. Additional elseif branches can used to avoid nesting the else blocks.

The condition value itself can optionally contain a variable declaration using let or var (including an unpacking declaration) allowing a computed value such as a regular expression match to be used as the condition without needing an extra declaration.

When expressions

A when-expression evaluates an expression and then evaluates a number of conditions in turn using the result, until one of the conditions returns non-nil. The corresponding block is then evaluated. If no condition returns non-nil then the else-block is evaluted if present, otherwise nil is returned.