LEVIATHAN vd52e79c4 · d52e79c486

Expressions

Indexing

The square-bracket operator reads and writes elements through an indexer accessor, and rebinds the variable when the target is an array.

since 0.1.0-alpha.1linuxwindowswasm

Description

a[i] reads an element and a[i] = v writes one. On a class, they call the indexer accessors declared with the selector ([]):

int get ([])(int i) => cells[i];        // read:  a[i] (an indexer declares its type)
set ([])(int i, int v) cells[i] = v;    // write: a[i] = v   (the value parameter is last)

An index meets its key type as an argument meets a parameter: a["x"] on an Array<int>, on a Map<int, V> or on an indexer that takes an int is a compile error.

On a mutable object the set accessor changes the object in place. On an array, which is a value, a[i] = v is shorthand for rebinding a to a new array whose slot i holds v; any other variable that held the old array is unaffected.

An indexer on a class, and arrays as values

use io::console;

class Grid {
    Array<int> cells = [0, 0, 0, 0];
    int get ([])(int i) => cells[i];
    set ([])(int i, int v) cells[i] = v * 10;
}

void main() {
    Grid g = Grid();
    g[1] = 4;
    console.writeln(g[1]);
    console.writeln(g[0]);

    Array<int> a = [1, 2, 3];
    Array<int> b = a;
    a[0] = 99;
    console.writeln(a);
    console.writeln(b);

    Map<string, int> m = Map();
    m["x"] = 1;
    console.writeln(m["x"]);
}
main();
40
0
[99, 2, 3]
[1, 2, 3]
1

Rules

  • a[i] calls the ([]) get accessor of a's class, and a[i] = v calls its ([]) set accessor.
  • On a mutable object the set accessor mutates the object in place.
  • On an array, a[i] = v rebinds a; a copy of the array taken earlier keeps its old contents.
  • When the element of an Array or a value of a Map is a struct, it is a place: a[i].x = v, the compound forms (a[i].x += v), a nested field (a[i].p.x = v) and a mutating call (a[i].bump()) read the element, change it and store it back exactly as a[i] = changed would, so the same rebinding rule applies. Every index on the way to the element is evaluated once, in order from the variable outward and before the right-hand side or the arguments, for every element type (the element order in calls), even when the element sits inside another element (grid[next()].cells[0].x += 1 calls next() once). Writing through an element whose index is outside the array throws, because reading the element throws, and nothing is stored.
  • Reading an index outside the array throws a RuntimeException. Reading a missing key with a map's at method throws as well. A plain store a[i] = v with an index outside the array stores nothing and does not throw; it never grows the array (use add).

Examples

An out-of-range index is a catchable exception

use io::console;

Array<int> xs = [1, 2, 3];
try {
    console.writeln(xs[5]);
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
caught: index 5 out of bounds (length 3)

See also

  • Array — An ordered sequence of values of one type, Array<T>, with value semantics.
  • Map — An associative collection from keys of type K to values of type V, with value semantics.