Notations for Collections

While the following is not central to the design of Grace, we do expect that users will frequently be working with collection classes like lists, sets, etc.

The first question, is how do you create one of these objects. In Java you would create a linked list of three integers as follows:

List lst = new LinkedList()
lst.add(1)
lst.add(2)
lst.add(3)

Note that Java does introduce a compact notation for declaring arrays:
Integer[] arr = {1,2,3}
but there is nothing equivalent to that for other collection classes.

Scala makes it easy:
var lst = List(1,2,3)

With Grace’s vararg feature (allowing a variable number of parameters at the end of the actual argument list), we could write:

var lst: List[Integer] := LinkedList[Integer].new(1,2,3)

or, with a small amount of type inference

var lst := LinkedList[Integer].new(1,2,3)

Python, as well as functional languages like ML and Haskell, allow programmers to create lists with a more compact notation: [1,2,3]

We expect to encourage a more listful style of programming with the use of higher-order functions like map, foreach, etc. Do we need to introduce a more compact notation for lists, like that in Python, or is the above fine?  What is the right answer given our target audience of first and second year students in CS.

As well as creating these collection classes, we also need to access elements. For linear sequences, we would like to access and update the ith element.

For Java arrays, we would write arr[i], while for lists we would write lst.get(i). The notation arr[i] can also be used on the left side of an assignment, while lst.set(i,newVal) results in replacing the ith element by newVal for a list.

In Scala, the access to the ith element is defined in an apply method, whose name can be omitted in a call. Thus, lst.apply(i) is the same as lst(i). An update method is used to provide a new value for the ith element (in a mutable collection).

Python accesses the ith element of a list with square brackets: lst[i], which can also be used on the left side of an assignment.

In Grace our current plans are to access elements using regular message sends where the message has a name like “at” or “get”. Another alternative might be to use an operator like “@” or use the [] notation like Python.

What do you think of these ideas? What choices are the simplest, most consistent, and/or most acceptable to our target oudience?