Ambiguous type variable error msg

前端 未结 5 568
终归单人心
终归单人心 2020-12-08 10:35

I don\'t think it is a bug, but I am a bit puzzled as to why that doesn\'t work. A bonus question is why does it mention variable e? There is no variable e.

    P         


        
5条回答
  •  轮回少年
    2020-12-08 11:12

    This problem shows up only in GHC 6.10; it can't be duplicated in GHC 6.8 because the type of handle is different:

    : nr@homedog 620 ; ghci
    GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
    Loading package base ... linking ... done.
    Prelude> :m +Control.Exception
    Prelude Control.Exception>  handle (\_ -> return "err") undefined
    "err"
    Prelude Control.Exception> 
    

    OK maybe I can get this right at last. I think the problem is not the monomorphism restriction, but rather you've hit an instance of the Read/Show problem: you're offering to handle some type of exception, in the new version of `handle, there is more than one type of exception, and the type of that exception does not appear in your result. So the compiler has no way of knowing which type of exception you're trying to handle. One way to work this is to pick one. Here's some code that works:

    Prelude Control.Exception> let alwaysError :: SomeException -> IO String; alwaysError = \_ -> return "err"
    Prelude Control.Exception> handle alwaysError undefined
    "err"
    

    Incidentally, the example use of handle in the GHC library documentation does not compile under 6.10. I have filed a bug report.

提交回复
热议问题