4 ms·
> Exactly as with pattern matching With pattern matching you'll get static checking if your matching is exhaustive. Impossible with an if ladder. Therefore th
by sklogic 11y ago
> Exactly as with pattern matching
With pattern matching you'll get static checking if your matching is exhaustive. Impossible with an if ladder. Therefore they're not semantically equivalent.
- dozzie 11y agoStatic checking of a variable, value of which is determined in runtime? How can you do that?
- dragonwriter 11y agoStatically checked pattern matching validates that the provided patterns cover all possible values the variable could have (the whole space of the variables declared/inferred type). So the actual runtime value is not necessary: if the match is exhaustive, there is no possible value that could be taken at runtime that would result in no match.
- sklogic 11y agotype 'a myOption = Some of 'a | None let f (Some x) = x*x Gives this output: Warning 8: this pattern-matching is not exhaustive. Here is an example of a value that is not matched: None
- dozzie 11y agoOh, now I see what you meant. But this has little to do with selecting the branch, what I was talking about the whole time.
- sklogic 11y agoWhy would you care about selecting the branch in compile time? We're talking about language features semantically equivalent to pattern matching, and, turns out, there are none. As for the importance of the exhaustion check, see the billion-dollar mistake. Your solution with type and value checks in an if ladder was called "dynamic typing" exactly for this reason - you select paths dynamically in the runtime without any static checks on soundness of this selection.
- dozzie 11y ago> Why would you care about selecting the branch in compile time? I wouldn't. Pattern matching is primarly a conditional, that's why I focused on if-else. > We're talking about language features semantically equivalent to pattern matching, and, turns out, there are none. Why would you think that pattern matching revolves around compile-time guarantees? It does not. It's primarly a conditional, everything else is an optional, additional effect. You can have pattern matching in dynamically typed languages (Lisp, Erlang).