Ambiguous type variable error msg

前端 未结 5 516
终归单人心
终归单人心 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:35

    "Exception e" is likely from the type signature of "handle".

    The documentation says:

    handle :: Exception e => (e -> IO a) -> IO a -> IO a
    

    In GHC 6.8 it used to be different, which would explain why I don't get that error.

    handle :: (Exception -> IO a) -> IO a -> IO a
    

    Seems you're running into the monomorphism restriction. That "_"-Pattern must be monomorphic (which it is with ghc 6.8) or explicitly typed. A "workaround" is to put the pattern on the left hand side of a definition, where it constitutes a "simple pattern binding" as specified by the Haskell Report.

    Try this:

    let f _ = return "err"
    handle f undefined
    

    http://www.haskell.org/haskellwiki/Monomorphism_restriction

提交回复
热议问题