简体   繁体   中英

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

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

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 . You need to create the UtilsDiff value first, then use that to create the Diff value.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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