简体   繁体   English

如何正确返回 haskell 中的嵌套类型?

[英]How to correctly return nested types in haskell?

I want to return empty lists if one of the parameters is Nothing, but I can't如果参数之一为 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
      ...

Apparently I haven't figured out exactly how to return the Diff type显然我还没有弄清楚如何返回 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 has type UtilsDiff -> UtilsOrDiff , not [a] -> [a] -> UtilsDiff . Diff的类型为UtilsDiff -> UtilsOrDiff ,而不是[a] -> [a] -> UtilsDiff You need to create the UtilsDiff value first, then use that to create the Diff value.您需要先创建UtilsDiff值,然后使用来创建Diff值。

Nothing -> return $ Diff (UtilsDiff [] [])

As the Just branch presumably evaluates to a Parser UtilsOrDiff value, so must the Nothing branch, hence the call to return .由于Just分支可能评估为Parser UtilsOrDiff值,因此Nothing分支也必须如此,因此调用return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM