You use at0 in OCL expressions when you need the element at a zero-based position in an ordered collection, especially a Sequence.
Syntax
self.at0(index : Integer) : T
| Part | Meaning |
|---|---|
self
|
The collection or Sequence from which to retrieve an element. |
index
|
The zero-based position to retrieve. 0 is the first position.
|
T
|
The type of the element returned from the collection. |
Use zero-based positions
at0 returns the element at the supplied zero-based index:
| Expression | Result |
|---|---|
cars.at0(0)
|
The first car |
cars.at0(1)
|
The second car |
cars.at0(2)
|
The third car |
For example, if cars is a Sequence containing Toyota, Volvo, and Saab in that order, cars.at0(0) returns Toyota and cars.at0(1) returns Volvo.
Retrieve an element from an instance collection
Use at0 when you need positional access. If your expression produces an instance collection, convert it to a Sequence before retrieving an element.
- Create or obtain the collection.
- Convert the collection to a Sequence with
asSequence()when needed. - Call
at0with the required zero-based position.
Example:
cars.asSequence().at0(0)
This expression returns the first element of the Sequence created from cars.
Pair at0 with indexOf0
Use indexOf0 and at0 together when you need to find an element's position and retrieve an element using the same index convention. Both use zero-based indexes.
For example, cars.indexOf0(Toyota) returns Toyota's zero-based position in the Sequence. You can use that position with cars.at0(...) to retrieve an element at a zero-based position.
Important: do not mix index bases
at0 is zero-based. The older indexOf operator is also documented as returning a zero-based position, while the at operator uses one-based indexing. Mixing indexOf or indexOf0 with at can therefore produce an off-by-one result.
Prefer at0 with indexOf0 so that the index convention is explicit throughout the expression.
