如果您查看
catches
的示例: f = expr `catches` [Handler (\ (ex :: ArithException) -> handleArith ex),
Handler (\ (ex :: IOException) -> handleIO ex)]
看起来catches
定义了一个自定义机制来匹配模式(两种异常类型)。我错了吗?或者这可以概括为允许定义一个函数,该函数可以接受能够匹配某个模式的lambda函数吗?
编辑:FYI下面是GHC catches的源代码。如果有人能够解释一下它是如何工作的,那就太好了。
catches :: IO a -> [Handler a] -> IO a
catches io handlers = io `catch` catchesHandler handlers
catchesHandler :: [Handler a] -> SomeException -> IO a
catchesHandler handlers e = foldr tryHandler (throw e) handlers
where tryHandler (Handler handler) res
= case fromException e of
Just e' -> handler e'
Nothing -> res