Conditional Scoped Variables
Pyro supports conditional-scoped variable declarations inside if
statements, e.g.
if var value = get_value(); value != null { echo "value: ${value}"; }
Here, value
is a new local variable valid inside the scope of the if
statement, including any else if
or else
clauses, e.g.
if var value = get_number(); value > 1000 { echo "very big number: ${value}"; } else if value > 100 { echo "big number: ${value}"; } else { echo "small number: ${value}"; }
Syntax
The syntax is:
if [let|var] <variable-declaration>; <expression> { ... }
The code inside the if
block will be executed if <expression>
is truthy.
Unpacking
You can use variable-unpacking syntax inside an if
statement, e.g.
if var (x, y) = get_location(); x == 1 && y == 2 { echo "correct location"; }
Here, x
and y
are new local variables valid inside the scope of the if
statement, including any else if
or else
clauses.