Subscript
The subscript postfix operation indexes into a slice or tuple value, returning the resulting value.
Process
At runtime:
- the operand is evaluated;
- the index expression is evaluated; and
- the element at the index is the subscript's result.
For slices, an index outside the slice's range is a runtime error; the compiler emits runtime logic to detect it, unless it can be proven that the index is within range. A negative index value is out of range.
A compile-time constant is an expression composed of integer literals, parenthesized expressions, and the arithmetic and negation operators, which the compiler evaluates at compile time. Arithmetic within a compile-time constant wraps as it would at runtime; a compile-time constant whose evaluation divides or takes a remainder by zero is a compile-time error.
See the runtime error model on the architecture page.
Assertions
- The operand's type is a slice type or a tuple type.
- The index expression has an integer type, signed or unsigned.
- For tuple operands, the index is a compile-time constant within the tuple's arity, and the subscript has the indexed element's declared type; a constant outside the tuple's arity is a compile-time error.
- For slice operands, the subscript has the slice's element type.
Syntax
subscript_operation = left_bracket, expression, right_bracket ;