Sequence Instantiation
Sequence instantiation creates a new instance of a
Sequence|Empty
Usage
Some simple examples using literals to specify the elements:
String[] none = {};
Integer[] ints = {1, 2, 3};
Sequence<String|Integer>|Empty mixed = {1, 2, "a few"};
Description
In some ways, sequence instantiation can be considered a form of invocation with sequenced arguments, and the expressions within the braces may be called the arguments to the instantiation.
Type inference
The typechecker inspects the arguments to a sequence instantiation:
- If there are none then the type is
Empty, - Otherwise it forms the
union type of the types of the
arguments as the type argument to the
Sequence.
This makes type inference (using value) work as you would expect,
for example:
// seq has type Sequence<Integer|Boolean|Float>
value seq = {1, true, 3.4}
// none has type Empty
value none = {}
Comprehension
A comprehension provides a convenient way of
instantiating a Sequence|Empty from (some of) the (possibly transformed)
elements returned from an Iterable.
// get the names of all planets without moons
value moonless = { for (p in planets) if (p.moons.size == 0) p.name };
See the reference on comprehensions for more details.
See also
- Comprehensions can be used to transform, filter and join Iterables in a sequence instantiation.