Case statement

This statement works only with enumerated values. You must specify a case for each enumeration value, there is no “default” or “fall through” possibility. This forces you to consider each possible value. Note that it is an error if any case specifies a value not supported by the enumeration. It is not possible for the value of the variable in the case header to not have a value consistent with its type.

case( in.valve position ) {
    .open : close -> my valve
    .closed : open -> my valve

Here is an example from the elevator case study for an up/down call at a subclass of Bank Level (floor) called a Middle Bank Level.

case (in.dir) {
    if (not Calling up)
        Calling up.set [call set]
    if (not Calling down)
        Calling down.set [call set]

[call set] /R38/Bank Level.Request cabin()

In the above example, an input parameter is passed in of type Direction which can be either up or down. The up case checks to see if the current floor has already requested service in the up direction with its boolean Calling up attribute set or not. If it is not already calling up, the boolean attribute is set to true and the call set guard is enabled. The down case is similar.

If the guard is enabled, a method on the superclass Bank Level is called.

Note that if 'in.dir' is not of type Direction, there will be a static error. Therefore, there is no need for a default or fall-through case.

Subclass cases

Sometimes it is helpful for a superclass to perform an activity differently based on its subclass instance. There is a system defined system defined _subclass name method, which returns an enumerated value corresponding to a subclass name. You can then switch based on the subclass as shown:

case ( _subclass name( R1 ) ) {
   .Fixed Wing Aircraft:
       // land on runway
   .Rotary Wing Aircraft:
       // land on helipad

The _subclass name method is invoked on the local instance (equivalent to me._subclass name). This method always returns an enumerated value whose type is automatically generated based on the set of subclass names for the specified generalization. If the local class is the superclass in only one generalization, you don’t need to specify it. Thus, case ( _subclass name ) will work if there is only one possible generalization.