You can use OclType in OCL to inspect the runtime model type of an object or value, especially when you need to diagnose or handle values from an inheritance hierarchy.
What OclType returns
OclType returns the type (class) of the value on its left. The returned type can be used as type information; use typeName or asString when you need its name as text.
| Expression | Result | Use |
|---|---|---|
value.OclType
|
The runtime type of value
|
Inspect or work with the type itself. |
value.OclType.typeName
|
The type name as a string | Display or diagnose the type name. |
value.OclType.asString
|
The type name as a string | Equivalent string representation for the type. |
Syntax
expression.OclTypeFollow OclType with an operation that uses the returned type, such as typeName or asString.
Inspect a value type
Use OclType.typeName to see what type an expression produced at runtime.
let vStringValue : String = 'SomeString' in
vStringValue.OclType.typeNameThis returns:
System.String
The following expression returns the same string value:
vStringValue.OclType.asStringFor the full behavior of these string representations, see typeName.
Use OclType when working with inheritance
When a variable can refer to instances from different classes, OclType lets you inspect the actual runtime class. For example, append .typeName to the expression you are investigating:
anObject.OclType.typeNameThis is useful while developing an expression: it tells you which class the current object has at runtime rather than relying only on the variable's declared context.
If your goal is to make a Boolean type test, use a type-testing operator instead of comparing displayed type names:
- Use oclIsKindOf when an object may be the specified type or a subtype of it.
- Use
oclIsTypeOfwhen you need an exact type check rather than an inheritance-aware check.
Convert between types and strings
OclType.asString converts type information to text. When you have a type name stored as text and need to use it as a model type again, use ClassFromString. This supports expressions that work with model metadata or type names kept in strings.
Find operators in the OCL editor
To explore the operators available for an expression, open the OCL editor and type a class or expression. See OCL General Operators for guidance on finding available operators.
