[Haskell-beginners] Non-exhaustive case leads to runtime error?

Daniel Fischer daniel.is.fischer at web.de
Tue Sep 9 16:37:37 EDT 2008


Am Dienstag, 9. September 2008 22:19 schrieb Rafal Kolanski:
> Greetings,
>
> I just ran into the following situation in an IO do block:
>          case msel of Just sel -> do toggleFretArea selectionRef sel
>                                      widgetQueueDraw canvas
>                       Nothing -> return ()
> except I omitted the last line. I expected the compiler to complain, but
> it silently compiled it, and I got a runtime exception when the program
> ran instead.
>
> Is this behaviour intentional? Can I get ghc to warn me about it anyway
> (something like -Wall)?

Yes, this is intentional, and yes, ghc can warn you about it, for example if 
you use -Wall. There's the more specific -fwarn-incomplete-patterns, too.
For a comprehensive list of eligible warnings, consult section 5.7 of the 
user's guide.
Neil Mitchell wrote a tool 'Catch', which is said to be even better at 
catching things like that (I've never tried it, so I don't know).

>
> Two more guiding questions:
> 1. What is a good idiom for "In this case do blah otherwise don't do
> anything"?

That depends on the situation. In monadic code, often you can use 'when' from 
Control.Monad:
when condition action

> 2. Is there a shorter way of writing a no-op in a do block than "return
> ()"?

As far as I know, there isn't.

>
> Thanks in advance!
>
> Rafal Kolanski.



More information about the Beginners mailing list