Haskell has a reputation for being a safe language. One that generally speaking pushes more of the possible programming errors to compile-time errors, and less and run-time.
One example of this is the if
expression. The else
in the if is always mandatory. You need to cover off both possibilities. This is great because you have thought and covered off all possibilities of what will happen at runtime.
Now Haskell has a case
expression. (This bears some similarity to switch
statements in other OO and imperative languages - but Haskell adds a lot of richness in the type system).
describeList :: [a]
describeList xs = "The list is " ++ case cs of [] -> "empty."
[x] -> "a singleton list."
xs -> "a longer list."
But with the case
expression, the default 'catch-all' is not mandatory.
To me this sounds like it would lead to runtime errors.
My question is: Why is the default catch-all not mandatory in a Haskell case
statement?
_ -> error "should never happen"
, but that's the default behaviour anyway. What's the point?xs -> "..."
is already a catch all - so is the question why you are not forced to use_
or is it why the warning you get when you don't match all is not an error?-Werror-on-incomplete-patterns
I would turn it on (-Werror
is overkill for me).