🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators asSequence
This page was created by Alexandra on 2017-08-13. Last edited by Wikiadmin on 2026-07-29.

You can use asSequence() in an OCL expression when you need an ordered Sequence, for example to access an item by its position with at0().

Syntax

expression->asSequence()

asSequence() returns a Sequence containing the elements of self.

A Sequence is an ordered collection and can contain duplicate elements. If the source expression already has an element order, asSequence() preserves that order when possible. Do not use this operator to create a meaningful order from an unordered source; it converts the collection type, but it does not define a new sort order.

When you apply asSequence() to one object, the result is a Sequence containing that one object.

When to use asSequence()

Use asSequence() when the position of an item matters.

For example, at0() uses zero-based indexing. Converting a collection to a Sequence lets you express that you are working with an ordered collection before retrieving its first item:

self.ValidMaterials->asSequence()->at0(0)

In this example, at0(0) returns the first element of the resulting Sequence. See at0() for indexing details.

Sets and Sequences

A Set contains no duplicate elements. A Sequence can contain duplicates and represents an ordered collection.

Converting a Set to a Sequence does not add duplicates that were not already present. It gives you a Sequence containing the Set's elements, but a Set does not itself provide a defined element order. If your expression depends on a particular position, ensure that the source has an order that can be preserved.

Requirement Use
You need unique elements and do not need positional access Set
You need an ordered collection or want to use a positional operator such as at0() asSequence()

Example

The following expression converts the material collection to a Sequence and retrieves its first element:

self.ValidMaterials->asSequence()->at0(0)

If self.ValidMaterials has an order, the resulting Sequence retains that order when possible. The expression therefore addresses the element at index 0, not a one-based “item number 1”.

For examples of creating collections of text values for use in OCL or EAL expressions, see Collection of strings.

See also