Array Access Routines

Sather - A Language Tutorial
Prev | Chapter 7. Operator Redefinition | Next

# 7.3. Array Access Routines

Sather supports the standard array access syntax of square brackets. For instance:

    a:ARRAY{INT} := |1,2,3|;
    a[2] := 5;             -- Sets the third element of the array to 5
    #OUT + a[0];           -- Prints out '1'
    c:ARRAY2{INT} := ||1,2,3|,|4,5,6|,|7,8,9||;
    #OUT + c[2,2];         -- Prints out '9'

However, the array bracket notation is not built into the array class. It is just a short hand for the routines `aget` and `aset`

    a[2] := 5;   -- equivalent to a.aset(2,5);
    #OUT + a[1]; -- equivalent to #OUT+a.aget(1);

Thus, classes which are not arrays can make use of the array notation as they please:

    class INT is
       -- The standard integer class
       aget(i:INT):BOOL is -- returns the 'i'th bit of the integer
    end;

In order for a class to actually have an array portion, it must inherit from `AREF{T}` (if it is a reference class) or `AVAL{T}` if it is a value class. The array setting notation is not as useful for immutable classes, since any modification of an immutable class must return a whole new object.

Prev | Home | Next
Operator expressions | Up | Immutable Classes