CUM 2: Lists

Some people claim that strings aren’t really convenient enough for all your data structure needs, so Comefrom0x10 should implement lists.

Status of this document

Draft, not yet accepted by CUP.

Syntax

Use square brackets, as this matches the state of the art and is unlikely to confuse programmers:

[item1, item2, etc]

Dereference an item in a list:

['a', 'b', 'c']@0 # extract the 'a' value

Even though languages much more commonly use square brackets to extract list items, that syntax is annoying and sure to be obsolete in future languages. The @ syntax only requires typing one character.

This requires some careful consideration of the precedence of @. It probably should be lower than all mathematical operators, for ease of using computed indices without requiring parens.

The @ operator probably should be right-associative. Consider:

['a','b']@[1,1,0]@2

Which would be nonsensical with left-associativity.

Naturally, retrieving values at nonexistent indices should be undefined:

[]@2 # undefined

Negative indexing should, of course, be allowed.

Slicing

Slices should use Python-style half open ranges and the syntax can be quite similar:

[]@start:end:stride

Immutability

All data types in Cf0x10 are currently immutable, for robustness [1]. Lists should be no different.

Relationship to strings

Indexing and slicing should also be implemented for strings.

Prior art

I think I’ve seen a language with a single-character list operator, but I can’t remember where.


[1]Not that immutability matters, since variables can, um, vary