Skip to content
ZOLO / TE156 Type · error

literal negative index on an array, tuple or string

Compiler diagnostic TE156: literal negative index on an array, tuple or string.

An index counts from 0, so a negative index on an array, a tuple or a string never names an element: xs[-1] is always out of range (an error at run time), and xs.get(-1) is always nil. Zolo rejects the literal form at compile time instead of letting it fail later.

let xs = [10, 20, 30]
print(xs[-1])
//       ^^ error[TE156]

Fix it

Count from the end with ^: ^1 is the last element, ^2 the one before.

let xs = [10, 20, 30]
print(xs[^1])          // 30
print(xs.get(^2))      // 20
print(xs[1..^1])       // [20]

zolo fix and the editor's quick fix rewrite -k to ^k.

A map is not a sequence: a negative key (m[-1] on a #{int: str}) is an ordinary key and is not reported.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en