Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.19.2

Conditional Expressions


Pyro supports two types of conditional expressions — if expressions and a ternary operator.

If Expressions

if expressions look like this:

var size = if value < 100 {
    "small"
} else {
    "large"
};

The syntax of an if expression is:

if <condition> {
    <value-if-true>
} else {
    <value-if-false>
}

An if expression is different from an if statement — an if expression requires an else clause and the {} blocks must each contain a single expression. (Note the lack of trailing semicolons inside the {} blocks.)

As you'd expect, if expressions support chained else if clauses, e.g.

var size = if value < 100 {
    "small"
} else if value < 1000 {
    "medium"
} else {
    "large"
};

The Ternary Operator

For simple use-cases, Pyro also supports a conditional or "ternary" operator, e.g.

echo value < 100 :? "small" :| "large";

The syntax for the ternary operator is:

<condition> :? <value-if-true> :| <value-if-false>

Conditional expressions using the ternary operator cannot be nested.