Indexing and Subranges

Indexing is done using the familiar bracket notation. Both Lists and Maps can be indexed and updated using this notation. If one attempts to index a non-list a Map conversion is first applied.

E[ E ]
E[ E .. E ]    # numbers only
      

Indexing in MillScript is 1-based rather than 0-based. Although is inconsistent with Java, which is a pity, our experience is that 1-based indexing is more commonly the "better" base (we guess around 70%:30%), works better with negative indices, and is more easily grasped by people working as occasional programmers.

Subranges work on Lists, or Objects converted to Lists such as Strings, and they return a sub-List of the original. Importantly, subranging preserves the original type, even after List conversion. For example, the subrange of a String is also a String.

Lists can be indexed using negative numbers. The last element of a list is, by convention -1, the second to last -2 and so on. These negative indices are particularly useful when writing subrange expressions such as "all but the last one" which becomes list[ 1 .. -2 ].