Control flow

if, while, for, ranges, break, continue and exhaustive pattern matching.

vlatest

PickleScript's control flow is statement-oriented. Conditions always go in parentheses and blocks always use braces.

if

fn classify(n: int) -> string {
    if (n < 0) {
        return "negative"
    } else if (n == 0) {
        return "zero"
    } else {
        return "positive"
    }
}

if is a statement. It produces no value, so there is no ternary and no if expression.

while

fn main() {
    var i = 0
    while (i < 3) {
        println("i = {i}")
        i += 1
    }
}

A while (true) loop is valid; use break to leave it.

for ... in

for iterates a range, a collection, a string, or anything implementing the iteration protocol:

fn main() {
    for (i in 0..3) {
        print("{i} ")     // 0 1 2
    }
    println("")

    for (name in ["ada", "grace"]) {
        println(name)
    }

    for (c in "abc") {
        print("{c}.")      // a.b.c.
    }
    println("")
}

There is no for (let i = 0; ...; ...) form. Use a while loop, or iterate a range.

Ranges

Expression Yields
0..3 0, 1, 2, end excluded
0..=3 0, 1, 2, 3, end included
fn main() {
    for (i in 0..=2) {
        print("{i} ")     // 0 1 2
    }
    println("")
}

A descending range literal such as 3..0 is simply empty, the half-open form assumes an ascending step. Use the range builtin when you need an explicit step, and a while loop when the bound is computed:

fn main() {
    for (i in range(5, 0, -2)) {
        print("{i} ")     // 5 3 1
    }
    println("")

    for (i in range(3)) {
        print("{i} ")     // 0 1 2
    }
    println("")

    for (i in range(2, 8, 3)) {
        print("{i} ")     // 2 5
    }
    println("")
}

C-style for

There is a second loop form with the three classic clauses. The initialiser must bind with var if the body is going to assign to it, and the step expression is an assignment rather than an operator like i++:

fn main() {
    var total = 0

    for (var i = 0; i < 5; i = i + 1) {
        if (i == 3) {
            continue
        }
        total = total + i
    }

    println(total)         // 7
}

Writing for (let i = 0; ...) and then assigning to i is E0362 cannot assign to immutable binding. +=, -=, *=, /=, %=, <<=, >>=, &=, |= and ^= all work in the step position.

break and continue

fn main() {
    for (i in 0..10) {
        if (i % 2 == 0) {
            continue
        }
        if (i > 7) {
            break
        }
        print("{i} ")      // 1 3 5 7
    }
    println("")
}

match and case

match is a statement and, in tail position, an expression. Each arm is case + pattern + -> + a braced body, and an empty arm is written {}. There is no default arm, the catch-all is the _ pattern.

fn grade(score: int) -> string {
    match (score) {
        case 100 -> { return "perfect" }
        case 0 -> { return "nothing" }
        case _ -> { return "somewhere" }
    }
}

The same function as a tail expression, with no return at all:

fn grade(score: int) -> string {
    match (score) {
        case 100 -> { "perfect" }
        case 0 -> { "nothing" }
        case _ -> { "somewhere" }
    }
}

A guard narrows a single arm. The arm is taken only when the extra condition holds, and matching then carries on with the arms below it:

fn classify(n: int) -> string {
    match (n) {
        case x if x < 0 -> { "negative" }
        case 0 -> { "zero" }
        case _ -> { "positive" }
    }
}

fn main() {
    println(classify(-5))     // negative
    println(classify(0))      // zero
    println(classify(7))      // positive
}

Matching is the natural way to consume an optional:

fn findFirst(xs: List<int>) -> int? {
    for (x in xs) {
        return x
    }
    return none
}

fn main() {
    match (findFirst([4, 5])) {
        case some(v) -> { println("first {v}") }
        case none -> { println("empty") }
    }
}

And the natural way to consume an enum. Note the qualified Shape.Circle: a variant is constructed and matched through the enum's name, and a variant that carries data declares named payload fields:

enum Shape {
    Circle(r: float)
    Rect(w: float, h: float)
}

fn area(s: Shape) -> float {
    match (s) {
        case Shape.Circle(r) -> { return 3.14159 * r * r }
        case Shape.Rect(w, h) -> { return w * h }
    }
}

fn main() {
    println(area(Shape.Circle(2.0)))
}

Because match is exhaustive, omitting an enum case is a compile error rather than a runtime surprise.

There is no if let. Bind the optional and match on it, as above. See Pattern matching.

Next steps