🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators Empty
This page was created by Peter on 2019-11-18. Last edited by Wikiadmin on 2026-07-29.

You can test whether a collection contains no elements by using isEmpty() in an OCL expression.

Use isEmpty() to test a collection

isEmpty() is a Boolean operation. It returns true when the collection has no elements; otherwise it returns false.

Use the collection operation syntax ->isEmpty().

Car.allInstances->isEmpty()

The expression returns true when no Car instances exist, and false when at least one Car instance exists.

Do not use empty()

Do not use empty() as a Boolean test. The documented MDriven OCL operation for this purpose is isEmpty().

Important: do not test a value type

Use ->isEmpty() only on a collection. Do not apply it to a value type. When a value type is used this way, OCL converts it to a list containing that value. That list has one item, so ->isEmpty() evaluates to false.

For example, a value such as an attribute value is not a collection to test with ->isEmpty().

Create an empty collection when needed

Testing whether a collection is empty is different from creating one. If an expression needs an empty collection of a specific class, use emptyList. For example:

Customer.emptyList

This creates an empty collection typed for Customer. You can then test that collection with ->isEmpty() or use it where a collection of Customer is required.

See also