Tagcase Statement

next up previous contents index
Next: Typecase Statement Up: Statements Previous: Continue Statement

Tagcase Statement

The tagcase statement is a special statement provided for decomposing "oneof" (B.13) and "maybe" (B.14) objects; it permits the selection of a body to be performed based on the tag of the object. Its form is
tagcase <expr> <tag_arm> <tag_arm>* [others ":" <body>] end
        <tag_arm> -> when <name> ["," <name>]* ["(" <idn> ":" <type_designator> ")"] ":" <body>
The expression must evaluate to a "oneof" or "maybe" object. The tag of this object is then matched against the names on the tag_arms. When a match is found, if a declaration exists in the arm, the value component of the object is assigned to the local variable idn. The matching body is then executed; the idn is defined only in that body. If no match is found, the body in the others arm is executed. When execution of the body completes, control continues at the statement after the tagcase.

In a syntactically correct tagcase statement, the following constraints are satisfied:

  1. The type of the expression is some "oneof" or "maybe" type T.

  2. The tags named in the tag_arms are a subset of the tags of T, and no tag occurs more than once.

  3. If all tags of T are present, there is no others arm; otherwise an others arm must be present.

  4. On any tag_arm containing a declaration, the type_designator must denote a supertype of the type that corresponds to each tag named in the tag_arm.

Here is an example:

        x: oneof[none: null, some: int]
        tagcase x
            when none:  ...
            when some(y: int):  ... y+7 ...