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:
pat => exp
|
| pat => exp
The type of each pattern may differ from each other. Variable patterns and anonymous patterns occurring in pat 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 of exp with each pattern from pat to pat in this order. Before trying matching with a pattern, is casted to the type of the pattern in the same way as the _dynamic expression. For the first matched pattern pat, it binds variables in pat to their corresponding part of the value and evaluates exp 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.