
On Jan 20, 2012 8:31 AM, "John Meacham"
As expected, no warnings. But if I change this "unfailable" code above to the following failable version:
data MyType = Foo | Bar
test myType = do Foo <- myType return ()
I *still* get no warnings! We didn't make sure the compiler spits out warnings. Instead, we guaranteed that it *never* will.
This is actually the right useful behavior. using things like
do Just x <- xs Just y <- ys return (x,y)
will do the right thing, failing if xs or ysresults in Nothing. for instance, in the list monad, it will create the cross product of the non Nothing members of the two lists. a parse monad may backtrack and try another route, the IO monad will create a useful (and deterministic/catchable) exception pointing to the exact file and line number of the pattern match. The do notation is the only place in haskell that allows us to hook into the pattern matching mechanism of the language in a general way.
John
I mention later that this is a "feature, not a bug" to some people, but I'm not one of them. The convenience of having this feature is IMO far outweighed by the cost of the runtime errors it can produce if you use the pattern matching in the wrong monad (e.g., IO, Reader, Writer...). Michael