使用`Control.Exception.catch'引起的

Jo0*_*0o0 2 haskell

当我加载haskell文件时,它有错误

module REPL(REPL(..), repl) where
import qualified Control.Exception as E
import System.Console.Readline(readline, addHistory)

data REPL s = REPL {
    repl_init :: IO (String, s),        -- prompt and initial state
    repl_eval :: s -> String -> IO (Bool, s),       -- quit flag and new state
    repl_exit :: s -> IO ()
    }

repl :: REPL s -> IO ()
repl p = do
    (prompt, state) <- repl_init p
    let loop s = (do
        mline <- readline prompt
        case mline of
        Nothing -> loop s
        Just line -> do
            (quit, s') <- repl_eval p s line
            if quit then
            repl_exit p s'
             else do
            addHistory line
            loop s'
        ) E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!"
        )
    loop state

REPL.hs:21:5:
    Couldn't match expected type `IO (Maybe String)'
           against inferred type `t -> Maybe String'
    In a stmt of a 'do' expression: mline <- readline prompt
    In the expression:
        (do { mline <- readline prompt;
              case mline of {
                Nothing -> loop s
                Just line
                  -> do { (quit, s') <- repl_eval p s line;
                          .... } } })
          E.catch
          undefined
          (\ (e :: E.SomeException) -> putStrLn "Handled exception!")
    In the definition of `loop':
        loop s = (do { mline <- readline prompt;
                       case mline of {
                         Nothing -> loop s
                         Just line -> do { ... } } })
                   E.catch
                   undefined
                   (\ (e :: E.SomeException) -> putStrLn "Handled exception!")
Run Code Online (Sandbox Code Playgroud)

Tho*_*son 10

通过在文件顶部包含此行来使用范围类型变量:

{-# LANGUAGE ScopedTypeVariables #-}
Run Code Online (Sandbox Code Playgroud)

或者在GHCi会议中,只需:

> :set -XScopedTypeVariables
Run Code Online (Sandbox Code Playgroud)

并在您的捕获功能提供签名.在GHCi中:

> import Control.Exception as E
> E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!")
Handled exception!
Run Code Online (Sandbox Code Playgroud)

庆祝美好时光,加油!