🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators insertAt
This page was created by Lars.olofsson on 2022-07-30. Last edited by Wikiadmin on 2026-07-29.

You can use insertAt in an OCL expression to place an object at a specific 1-based position in a collection or ordered association.

Syntax

collection->insertAt(index, object)

insertAt has the following signature:

insertAt(index : Integer, object : T) : Sequence(T)

It returns a Sequence(T) containing the elements of self, with object inserted at index. T means the type of object held by the collection.

Use 1-based positions

The index is 1-based. Position 1 is the first position, not the zero position.

For example, this expression inserts 'EmailAddress' as the second item:

Set{'String', 'PhoneNumber'}->insertAt(2, 'EmailAddress')

The resulting sequence has the order:

'String', 'EmailAddress', 'PhoneNumber'

Use Set when you need to create a collection literal for an expression. The result of insertAt is a Sequence so that the inserted position is represented by its order.

Insert into an association

You can use insertAt on an association when you need a related object to occupy a particular position rather than be added at the end.

This is meaningful only for an ordered association. An unordered association has no defined position, so there is no position for insertAt to control.

For example, if self.Items is an ordered association, the following places newItem at the first position:

self.Items->insertAt(1, newItem)

Use insertAt when the position matters. Use an add operation when you want the object at the end and do not need to choose its position.

See also