[Haskell-cafe] exceptions vs. Either

Ketil Malde ketil+haskell at ii.uib.no
Tue Aug 3 14:48:22 EDT 2004

MR K P SCHUPKE <k.schupke at imperial.ac.uk> writes:

> head :: [a] -> Maybe a
> head (a0:_) = Just a0
> head _ = Nothing

Argh, no!  Violating the precondition of head is a bug in the caller,
I want it to crash, but I also want to know where.  Wrapping it up in
Maybe (or any other error propagation) is not a solution. 

I don't want to write a lot of unwrapping code in all the callers,
just to get a trackable error message in the (few) cases where I'm
using the function incorrectly.

Neither do I want to write

        (\x -> if null x then error ("__FILE__:__LINE__") else head x)

everywhere instead of head, nor pollute my code with error tracking, but
otherwise meaningless strings. (Which is what I generally do when I
get this kind of anonymous error.  if null x then error "foo" else
head x.

A mechanism for a function to report the caller site would obliviate
the need for all this ugliness.


Hmm...if I run it through CPP and 
  #define HEAD (\x -> if null x then error ("__FILE__:__LINE__") else head x)
is the __LINE__ resolved at the place of declaration or at the place of usage?
If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell-Cafe mailing list