[Haskell-beginners] Ambiguous type variable
michael at snoyman.com
Thu Aug 17 13:36:49 UTC 2017
On Thu, Aug 17, 2017 at 4:29 PM, Francesco Ariis <fa-ml at ariis.it> wrote:
> On Thu, Aug 17, 2017 at 12:24:07PM +0000, Jonathon Delgado wrote:
> > I'm sure it makes sense! I'm not really following though.
> > I understood typeclasses to be analogous to OO interfaces. So if a
> > implements the Exception interface, and Exception implements the Show
> > interface, then it should automatically support show.
> > I take it this was wrong? How does the compiler use typeclasses if
> > not interfaces?
> 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?
> 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`
> 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`.
This is dangerous: `catch` with `e :: SomeException` will catch all
asynchronous exceptions, breaking things like timeout, race, and the async
library in general. That's why my message about mentioned the
> Ask more if needed!
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners