🚀 Welcome to MDriven Learn –  Don’t miss the latest Release Notes.
OCLOperators casetruefalse

This is a new compact form to write:

if somebool then
  someexpressionwithtypeX
else
  someOtherexpressionwithtypeX
endif

This operator enables you to write the same thing as:

somebool.casetruefalse(someexpressionwithtypeX,someOtherexpressionwithtypeX)

Usage in EAL

Use this if you want to execute methods in a case/switch-like statement.

This makes a switch-like construct look like this:

let x=SomeObject.SomeEnum in
(
  (x=#Enum1).casetruefalse(SomeObject.DoYourThing1,DoNothing);
  (x=#Enum2).casetruefalse(SomeObject.DoYourThing2,DoNothing);
  (x=#Enum3).casetruefalse(SomeObject.DoYourThing3,DoNothing)
)

Usage in OCL

Use this if you want to derive a string based on a list of conditions and have an else-statement if none of the expressions were True:

(self.oclIsInState(#New).caseTrueFalse('New/Ordering', String.nullValue)+
 self.oclIsInState(#Waiting).caseTrueFalse('Waiting', String.nullValue)+
 self.oclIsInState(#Ordered).caseTrueFalse('Ordered', String.nullValue))->
collect(s| s.notNull.caseTrueFalse(s, self.State))->first

The collect construct at the end can return the result and, if it is null, execute the "else" statement. Note: Parentheses around the string concatenation.

See also: OCLOperators_whentrue