You use last() in OCL to retrieve the final element of a collection; it is for expressions where the collection order is meaningful.
Syntax
collectionExpression.last()
last() returns a value of type T, where T is the type of element in the collection.
How it works
The operator evaluates its operand as a list and returns the last element of that list. This means you can apply it to a collection expression rather than needing to explicitly create a list first.
For example, if a Fruit object has a collection of fruits, the following expression returns the final fruit in that collection:
self.Fruits.last()
If the collection contains, in order, Apple, Banana, and Orange, the expression returns Orange.
Use it when collection order matters
last() selects by position, not by a property value. Use it when the collection already has the order that your expression requires.
For example, this expression obtains the name of the final fruit:
self.Fruits.last().Name
Before using last(), make sure that the collection's order represents what âlastâ means in your model. For example, a collection may need to be ordered by a date before its last element can represent the most recent item.
Related operators
Use maxValue when you need the highest value in a collection of simple values rather than the final element by collection position. See Documentation:OCL General Operators to find and work with available OCL operators.
