簡體   English   中英

如何正確返回 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM