Ambiguous type variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Ambiguous type variable

Jonathon Delgado
I'm trying to use
  catch (...) (\e -> putStrLn $ show e)
However, I get an error
  Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the constraint ‘(Show a0)’ from being solved.
This goes away if I change the code to
  catch (...) (\e -> putStrLn $ show (e::IOException))

A couple of things I don't understand here:
- The signature for catch begins "Exception e", and exception it "class (Typeable e, Show e) => Exception e". So why isn't show automatically available?
- Why does the new code work at all? e is Exception, not IOException. What would happen if it caught a different Exception?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguous type variable

Francesco Ariis
On Thu, Aug 17, 2017 at 07:55:28AM +0000, Jonathon Delgado wrote:

> I'm trying to use
>   catch (...) (\e -> putStrLn $ show e)
> However, I get an error
>   Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the constraint ‘(Show a0)’ from being solved.
> This goes away if I change the code to
>   catch (...) (\e -> putStrLn $ show (e::IOException))
>
> A couple of things I don't understand here:
> - The signature for catch begins "Exception e", and exception it "class (Typeable e, Show e) => Exception e". So why isn't show automatically available?
> - Why does the new code work at all? e is Exception, not IOException. What would happen if it caught a different Exception?

IOException is a concrete type while Exception is a typeclass. In the end,
the compiler needs the former, the latter not being enough.

The code works as any other class-based function would

    someFunction :: Monoid a -> [a] -> a
    -- ^-- in the end `Monoid a` will become something concrete, like
    -- a String, a Sum, etc.

Does that make sense?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguous type variable

Michael Snoyman
In reply to this post by Jonathon Delgado


On Thu, Aug 17, 2017 at 10:55 AM, Jonathon Delgado <[hidden email]> wrote:
I'm trying to use
  catch (...) (\e -> putStrLn $ show e)
However, I get an error
  Ambiguous type variable ‘a0’ arising from a use of ‘show’ prevents the constraint ‘(Show a0)’ from being solved.
This goes away if I change the code to
  catch (...) (\e -> putStrLn $ show (e::IOException))

A couple of things I don't understand here:
- The signature for catch begins "Exception e", and exception it "class (Typeable e, Show e) => Exception e". So why isn't show automatically available?
- Why does the new code work at all? e is Exception, not IOException. What would happen if it caught a different Exception?


In my experience, the most common thing people need is "catch all synchronous exceptions." The word "synchronous" there is the source of a lot of confusion, and relates to a complicated topic of asynchronous exceptions. My recommendation is: don't worry about that right now, use the safe-exceptions package, and switch from catch to catchAny. More details on the package are available at:


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners