Grace 0.7 introduces Lineups – a built-in constructor for immutable iterables. So [1, 2, 3] is an iterable with three elements. This will enable us to remove variable-arity methods. For example, the constructors for Set, List, etc in the collections library will now take a single iterable rather than a variable number of arguments.
On the inside, methods that take an iterable as an argument are not very different from those with a variable number of parameters. The method declaration is different, because the parameter is declared as xs:Iterable<Number>
instead of *xs:Number
, but we think that this is clearer. In both cases, the type of xs
is such that the body of the method can iterate over it and deal with one Number
at a time.
12345 method addAll (xs:Iterable) {for (xs) do { x ->self.add(x)}}
From the outside (the requestor’s view), the main change is that variable-arity method requests like
1 directions("north", "south", "east", "west")
will be replaced by single argument requests like
1 directions ["north", "south", "east", "west"]
You can use iterable constructors directly, for example, to initialise variables or constants
12 def directions = [ "north", "south", "east", "west" ]var listOfNames := [ ]
but, because we intentionally provide Iterables with a limited protocol, it’s more typical to use them to create collection objects:
123 def myList = list [1, 2, 4, 8]def mySequence = sequence [2, 3, 5, 7, 11]def mySet = set ["a", "e", "i", "o", "u"]
The old design using variable-arity methods initially seemed more “neutral”, because it did not require honouring one particular kind of collections with the privileged of being built in, while others were relegated to a library. However, if the truth be told, both designs rely on a built-in implementation of some sort of collection. The old design created these collections implicitly whenever a request was received by a variable arity method; the new design creates the Iterable explicitly before the request is made. Escape analysis should permit both designs to be optimised either in a JIT or by a whole-program compiler, should that become an issue. But the new design makes it much simpler to check that methods are requested with the correct number of arguments.