SML# Document Version 4.1.0
19 Expressions

19.25 Case branch expression with dynamic type cast _dynamiccase ⟨exp⟩ of ⟨match⟩

This expresses general conditional branches on the dynamically-typed value of ⟨exp⟩. ⟨match⟩ is of the form:

  ⟨pat1⟩ => ⟨exp1⟩
| ⋯
| ⟨
patn⟩ => ⟨expn⟩

The type of each pattern may differ from each other. Variable patterns and anonymous patterns occurring in ⟨pati⟩ must be type-annotated. In addition, the set of patterns must satisfy the following restriction:

  • •

    When categorizing the set of patterns by their types, for each set of patterns of the same type, the set of patterns must satisfy the same condition as the case expression.

For example, the following is redundant on int patterns and therefore violates the rule.

# fn x => _dynamiccase x of x:int => "int" | x:real => "real" | 0:int => "zero";
(interactive):1.8-1.76 Error: match redundant
      x => ...
  --> 0 => ...

This expression is evalated as follows. It tries to match the dynamically-typed value v of ⟨exp⟩ with each pattern from ⟨pat1⟩ to ⟨patn⟩ in this order. Before trying matching with a pattern, v is casted to the type of the pattern in the same way as the _dynamic expression. For the first matched pattern ⟨pati⟩, it binds variables in ⟨pati⟩ to their corresponding part of the value and evaluates ⟨expi⟩ to the result value. If no dynamic type cast succeeds, it raises the Dynamic.RuntimeTypeError exception. If no pattern matches with the value, it raises the Match exception.

To use this expression in the separate compilation mode, "reify.smi" must be _required.