This is much like the familiar switch, except the case-clauses have been replaced with match-clauses, and there's no longer a need for that irritating break to prevent fall-through. (There is also no longer the ability to fall through, but this seems like a good trade.) The specimen-expression is evaluated once, and the resulting value is matched against each pattern from top to bottom until a match is found. On the first match, the corresponding body-expression is evaluated, and its outcome is the outcome of the switch. If no patterns match, a to-be-specified*** exception is thrown. To say the equivalent of "case constant" use "match ==constant", since the "==" pattern only matches if the specimen is the same as the expression on the right. To say the equivalent of "default", use "match _ " as the last matcher, since it will match everything not previously matched. *** forget the expansion, or rather, postpone to appendix Switch is precicely defined by this expansion to Kernel E: { def temp := specimen-expression if (temp =~ pattern1) { body-expression1 } else if (temp =~ pattern2) { body-expression2 } ... { ... } else { throw(...) #Exception to be specified } but implementations are encouraged to optimize those uses equivalent to "case constant" and "default". |
||||||||||||
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
|