You use append in OCL when you want a new sequence with one object placed after the existing elements, without changing the source collection.
Syntax
sequence->append(object)append has the signature:
append(object : T) : Sequence(T)It returns a Sequence that contains every element in the source collection (self), followed by object as the last element.
How append works
Use append when the position of the new item matters and it must appear last in the result.
Sequence{1, 2, 3}->append(4)The expression returns:
Sequence{1, 2, 3, 4}The source sequence remains unchanged. In the example, Sequence{1, 2, 3} is still the original sequence; the appended sequence exists only as the result of the expression.
Use the returned sequence
Because append does not update its source, use its return value where you need the extended sequence.
For example, append an item before processing the complete result:
Sequence{1, 2, 3}->append(4)->collect(i | i * 2)This processes the sequence Sequence{1, 2, 3, 4}. It does not add 4 to a stored collection or association.
append does not change an association
Do not use append when you need to add an object to an association or otherwise change the underlying collection. Use add for that purpose.
| Requirement | Use | Result |
|---|---|---|
| Produce a new sequence with an object at the end | ->append(object)
|
Returns a Sequence; source remains unchanged.
|
| Add an object to an association or change the underlying collection | .add(object)
|
Changes the sequence or association. |
Related collection operations
- Use
prependwhen the object must come before the existing elements. - Use
excludingwhen you need a result that omits an object while leaving the source collection unchanged. - Use
Set{...}to create a collection with no duplicates. Use a sequence when order is significant.
