[Haskell-beginners] Ambiguous type variable
Jonathon Delgado
voldermort at hotmail.com
Thu Aug 17 14:18:41 UTC 2017
Thank you very much, that does clarify things!
Francesco Ariis wrote:
That's correct! Indeed ghc is not complaining about a lack of
instances, as it would with, say
λ> putStrLn 5
-- • No instance for (Num String) arising from etc etc.
but about the *ambiguity* of type variable `e`. How does `catch` know
_which_ exception to deal with if you don't specify the concrete type?
Consider:
prova = catch (print $ div (error "hey bby") 0)
(\e -> print "ouch" >>
print (e :: ErrorCall))
-- I want to deal with arithmetic errors here and not in `prova`
palla = catch prova
(\e -> print "baaah" >>
print (e :: ArithException))
If I switch ArithException and ErrorCall the behaviour of invoking `palla`
changes.
Having a catch-all `catch` is possible by using (e :: SomeException);
if you don't care about `e` and just want to do an action regardless, you
are probably better off with `onException`.
Ask more if needed!
More information about the Beginners
mailing list