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

You use emptyList in an OCL expression when you need an empty collection whose element type is known, for example an empty collection of Customer objects.

Create a typed empty collection

OCL is strongly typed. An empty collection has no elements from which OCL can infer a type, so you create it from the class that the collection is intended to contain.

ClassName.emptyList

Replace ClassName with a class in your model. For example, this expression creates an empty collection intended to contain Customer instances:

Customer.emptyList

The result is an empty Collection(Customer). You can use collection operators on it, such as ->count or ->first, without a type error caused by an unknown element type.

Use emptyList in a conditional expression

Use emptyList when one branch of a conditional must return no objects and the other branch returns objects of a known class. Both branches then return collections of the same element type.

For example, return customers with a balance above 100 only when the condition is true:

if includeCustomers then
  Customer.allInstances->select(c | c.Balance > 100)
else
  Customer.emptyList
endif

When includeCustomers is false, the result is an empty collection of Customer. When it is true, the result contains the matching Customer instances.

Combine an empty list with other collections

You can use a typed empty list as the starting value for a collection expression. In this example, myResults starts as an empty customer collection and is combined with customers whose balance exceeds 100:

let myResults = Customer.emptyList in
  myResults->union(
    Customer.allInstances->select(c | c.Balance > 100)
  )

If customers match the selection, the result contains those customers. If none match, the result remains an empty collection of Customer.

Do not use emptyList to test a collection

emptyList creates a new empty collection; it does not inspect an existing value. To test whether an existing collection has elements, use isEmpty or notEmpty. Do not apply those operators to a value type: a value type is converted to a one-item list, which can produce an unexpected result.

Related collection expressions

For a collection with specified values rather than no values, use Set. For examples of constructing collections of text values, see Documentation:Collection of strings.

See also