Exception 为什么模式匹配不会在Maybe monad中抛出异常
我的问题很简单。为什么错误的模式匹配不会在monad中引发异常。为清楚起见:Exception 为什么模式匹配不会在Maybe monad中抛出异常,exception,haskell,monads,Exception,Haskell,Monads,我的问题很简单。为什么错误的模式匹配不会在monad中引发异常。为清楚起见: data Task = HTTPTask { getParams :: [B.ByteString], postParams :: [B.ByteString], rawPostData :: B.ByteString } deriving (Show) tryConstuctHTTPTask :: B.ByteString -> Maybe Task tryConstuctHTTPTas
data Task = HTTPTask {
getParams :: [B.ByteString],
postParams :: [B.ByteString],
rawPostData :: B.ByteString
} deriving (Show)
tryConstuctHTTPTask :: B.ByteString -> Maybe Task
tryConstuctHTTPTask str = do
case decode str of
Left _ -> fail ""
Right (Object trie) -> do
Object getP <- DT.lookup (pack "getParams") trie
Object postP <- DT.lookup (pack "postParams") trie
String rawData <- DT.lookup (pack "rawPostData") trie
return $ HTTPTask [] [] rawData
data Task=HTTPTask{
getParams::[B.ByteString],
postParams::[B.ByteString],
rawPostData::B.ByteString
}派生(显示)
tryconstuchttptask::B.ByteString->Maybe Task
tryconstuchttptask str=do
案例解码
左->失败“”
右(对象trie)->do
Object getPDoingpattern@Anton:您可以在的源代码中找到它,但它的定义只是fail\uu=Nothing
。我没有找到模式匹配过程。只有(仅x)> = k= k席想查看定义这种行为的源代码。谢谢。@Anton:正如我的回答所解释的,这里有两种行为是相关的:a)在Maybe
monadfail foo
中返回Nothing
。定义此行为的源代码只是Monad Maybe
的实例声明中的fail\u=Nothing
。b) 使用@Anton进行失败的模式匹配:如果您想查看实现标准中指定翻译的源代码,您必须深入研究ghc
源代码(或您正在使用的Haskell编译器的源代码)。
expression >>= \x ->
case x of
pattern -> ...
_ -> fail