[英]How to return different types in the same function in haskell?
[英]How to correctly return nested types in haskell?
如果參數之一為 Nothing,我想返回空列表,但我不能
data UtilsDiff = UtilsDiff {syntaxBlockOptions :: [String], optionsBlockOptions :: [String]}
deriving Show
data UtilsOrDiff = UtilsOrDiff Utils | Diff UtilsDiff
deriving Show
useBlockExpr :: Parser UtilsOrDiff
useBlockExpr = do
u1 <- syntaxExpr
opts <- optionsBlockExpr
_ <- absorbToStopWord "Description:"
utilDesc <- many anyChar
case options u1 of
Nothing -> Diff [] [] -- <- this line
Just val-> do
...
顯然我還沒有弄清楚如何返回 Diff 類型
warning: [-Wdeferred-type-errors]
• Couldn't match expected type ‘[a0] -> Text.Parsec.Prim.ParsecT String () Data.Functor.Identity.Identity UtilsOrDiff’ with actual type ‘UtilsOrDiff’
• The function ‘Diff’ is applied to two value arguments,but its type ‘UtilsDiff -> UtilsOrDiff’ has only one
In the expression: Diff [] [] In a case alternative: Nothing -> Diff [] []
Diff
的類型為UtilsDiff -> UtilsOrDiff
,而不是[a] -> [a] -> UtilsDiff
。 您需要先創建UtilsDiff
值,然后使用它來創建Diff
值。
Nothing -> return $ Diff (UtilsDiff [] [])
由於Just
分支可能評估為Parser UtilsOrDiff
值,因此Nothing
分支也必須如此,因此調用return
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.