Golo support special support for common collections. The syntax uses brackets prefixed by a collection name, as in:
let s = set[1, 2, "a", "b"] let v = vector[1, 2, 3] let m = map[[1, "a"], [2, "b"]] # (...)
The syntax and type matchings are the following:
Collection | Java type | Syntax |
---|---|---|
Tuple |
|
|
Array |
|
|
List |
|
|
Vector |
|
|
Set |
|
|
Map |
|
|
Tuples essentially behave as immutable arrays.
The gololang.Tuple
class provides the following methods:
get(index)
method to get the element at a specified index,
size()
and isEmpty()
methods that do what their names suggest,
iterator()
method because tuples are iterable, and
equals(other)
, hashCode()
and toString()
do just what you would expect.
The map collection literal expects entries to be specified as tuples where the first entry is the key, and the second entry is the value. This allows nested structures to be specified as in:
map[ ["foo", "bar"], ["plop", set[1, 2, 3, 4, 5]], ["mrbean", map[ ["name", "Mr Bean"], ["email", "bean@outlook.com"] ]] ]
There are a few rules to observe:
Because of that, the following code compiles but raises exceptions at runtime:
let m1 = map[1, 2, 4, 5] let m2 = map[ [1], ["a", "b"] ]
The rationale for map literals to be loose is that we let you put any valid Golo expression, like functions returning valid tuples:
let a = -> [1, 'a'] let b = -> [2, 'b'] let m = map[a(), b()]