Conditions and loops

Rook — 1.0 specification

if

if x > 0 {
    console.log("positive")
} else if x < 0 {
    console.log("negative")
} else {
    console.log("zero")
}

No parentheses around the condition, braces are required. The condition must be a bool: unlike JS, there is no truthy/falsy. if count { is an error, write if count > 0 {, and instead of if user { write if user != null {.

An object literal in the header of if, for or switch goes in parentheses, or its { would be taken for the start of the block (the same rule as in Go).

Experimental: the initializer of if and switch; the rest of if is stable.

An initializer goes before the condition, as in Go: a const or let declaration and ;. Its variables are seen in the condition and in every branch, else included, and not after the if, so a value that only the if needs does not stay in the function:

if const user, err = load(id); err != null {
    console.log(err.message)
} else if let age = user.age; age >= 18 {
    console.log(`${user.name}, ${age}`)
}
{
  const [user, err] = load(id)
  if (err != null) {
    console.log(err.message)
  } else {
    let age = user.age
    if (age >= 18) {
      console.log(`${user.name}, ${age}`)
    }
  }
}

The condition narrows the variables, as any condition does: in if const u = find(); u != null, u is not null in the branch. switch takes an initializer in the same way (see switch); the {if} of markup does not: declare the value before the markup.

for, the only loop

Rook JS
for let i = 0; i < n; i++ { } for (let i = 0; i < n; i++) { }
for i in 0..n { } for (let i = 0, $$end = n; i < $$end; i++) { }
for x in items { } for (const x of items) { }
for i, x in items { } (an array) for (let i = 0; i < items.length; i++) { const x = items[i] }
for k, v in scores { } (a Map) for (const [k, v] of scores.entries()) { }
for v in scores { } (a Map) for (const v of scores.values()) { }
for v in seq { } (an iterator) for (const v of seq()) { }
for line in lines { } (async) for await (const line of lines) { }
for cond { } while (cond) { }
for { } while (true) { }

An array or a typed array with an index goes over its indexes, which is faster than over the pairs of entries(): the array is read once, as entries() would read it, so a loop that assigns the variable of the array goes on over the old one. Without the index, for _, x in items, it is for...of.

A range, for i in 0..n, goes over the numbers from the first up to the second, without it: 0..3 gives 0, 1 and 2, and 5..2 nothing. Both ends are numbers and are computed once, before the loop, as in Go: a loop that changes n goes on to the old end. The variable is a number that the body cannot assign, as any variable of for ... in. A range is written only after in, with one variable (for i, x in 0..n is an error), and not with go for, which goes over an array. In {for} of markup it is the array of the numbers, $$numbers(0, n).

for x in items goes over the values, like for...of in JS or for x in in Python and Rust, not over the keys like for...in in JS. In Go, for i := range xs with one variable gives the index; here one variable gets the value, and two get the index and the value.

break and continue work as usual; to leave a loop around the one they are in, give it a label (see "Labels").

Iterators

func count(n number) func(yield func(number) bool) {
    return func(yield func(number) bool) {
        defer console.log("done")
        for let i = 0; i < n; i++ {
            if !yield(i) {
                return
            }
        }
    }
}

for i in count(10) {
    if i == 3 {
        break
    }
}
const firstThree = count(1_000_000)().take(3).toArray()
function count(n) {
  return function* () {
    try {
      for (let i = 0; i < n; i++) {
        yield i
      }
    } finally {
      console.log('done')
    }
  }
}

for (const i of count(10)()) {
  if (i === 3) {
    break
  }
}
const firstThree = count(1_000_000)().take(3).toArray()

An iterator is a function whose one parameter is named yield, as in Go 1.23: func(yield func(T) bool) gives values of T, and func(yield func(K, V) bool) gives pairs. It calls yield(v) for each value; for v in seq goes over them and for k, v in seq over the pairs, where one variable gets the second of a pair, as with a Map.

switch

switch cmd {
case "start", "run":
    start()
case "stop":
    stop()
default:
    console.log("unknown command")
}
switch (cmd) {
  case 'start':
  case 'run':
    start()
    break
  case 'stop':
    stop()
    break
  default:
    console.log('unknown command')
}

Every case ends with an implicit break; several values are separated by commas.

switch without an expression replaces a long chain of ifs:

switch {
case score >= 90:
    grade = "A"
case score >= 75:
    grade = "B"
default:
    grade = "C"
}
if (score >= 90) {
  grade = 'A'
} else if (score >= 75) {
  grade = 'B'
} else {
  grade = 'C'
}

switch takes an initializer as if does, switch const kind = node.kind; kind { ... }, also without an expression: switch const n = items.length; { case n > 10: ... }.

Labels

Experimental: may change between releases; see the status at the top.

A label before for, switch or select names the statement for break and continue inside nested loops, as in Go and JS:

outer: for row in grid {
    for v in row {
        if v < 0 {
            continue outer                 // the next row
        }
        if v == target {
            console.log("found")
            break outer                    // both loops
        }
    }
}
outer: for (const row of grid) {
  for (const v of row) {
    if (v < 0) {
      continue outer
    }
    if (v === target) {
      console.log('found')
      break outer
    }
  }
}

Markdown source on GitHub