Sequences
Sequence Syntax
Sequences are variable-length collections of elements:
module M
{
sequence<Fruit> FruitPlatter;
}
A sequence can be empty — that is, it can contain no elements, or it can hold any number of elements up to the memory limits of your platform.
Sequences can contain elements that are themselves sequences. This arrangement allows you to create lists of lists:
module M
{
sequence<FruitPlatter> FruitBanquet;
}
Sequences are used to model a variety of collections, such as vectors, lists, queues, sets, bags, or trees. (It is up to the application to decide whether or not order is important; by discarding order, a sequence serves as a set or bag.)
Language Mapping
Here is the definition of our FruitPlatter sequence once more:
sequence<Fruit> FruitPlatter;
The Slice compiler generates the following definition for the FruitPlatter sequence:
public typealias FruitPlatter = [Fruit]
As you can see, the sequence simply maps to a standard array, so you can use the sequence like any other array. For example:
// Make a small platter with one Apple and one Orange
//
let platter: FruitPlatter = [.Apple, .Orange]
There is a single exception to this mapping rule: a sequence of bytes maps to Foundation.Data and not [UInt8]:
sequence<byte> ByteSeq;
becomes:
public typealias ByteSeq = Foundation.Data