4 ms·
if-statement dispatching is not pattern matching. Pattern matching yields more type information than if statements and can match recursively on multiple argume
by Peaker 14y ago
if-statement dispatching is not pattern matching.
Pattern matching yields more type information than if statements and can match recursively on multiple arguments (which even multi-method dynamic dispatch cannot). However, it always dispatches on a closed sum type, whereas OOP-style dynamic dispatch is on an open sum type, so the mechanisms are useful in different circumstances.
- colomon 14y agoCan you expand on this difference?
- Peaker 14y ago"If/switch" branching is sometimes called "boolean blindness": http://existentialtype.wordpress.com/2011/03/15/boolean-blindness/ http://existentialtype.wordpress.com/2011/03/15/boolean-blin... The reason is that no new type information is gained when you branch. When you pattern match, however, you gain new names in scope that have new types. This is new type information such that the branch choice represents new information not only in the program position, but also at the type level. For example: if(x != NULL) { .. compiler does not know if x is null or not .. } else { .. ditto } Whereas with pattern matching: case x of Nothing -> .. Scope gets no new value. x is a Maybe type, not usable as a direct value Just y -> .. Scope gets "y" as a value of the type inside the maybe, which is directly usable. Also, you can define a function like: f (Just (Right x)) (y:ys) = ... f (Just (Left e)) [] = ... f _ xs = ... which pattern-matches multiple arguments at the same time, including recursive pattern matching (Right inside Just, Left inside Just, etc). If you meant the difference regarding open/closed sum types, I can expand on that.