MixFix Chanel
MixFix Chanel

Reputation: 13

Couldn't match type ‘Maybe’ with ‘IO’

This is my function

getDateFromUser :: IO (Either UserError Day)
getDateFromUser = do
  Prelude.putStrLn "Пожалуйста, укажите дату для прогноза в формате ГГГГ-ММ-ДД:"
  currentTime <- getCurrentTime
  date <- Prelude.getLine
  let retrievedDate = utctDay currentTime
      dayFromUser   = parseTimeM True defaultTimeLocale "%Y-%-m-%-d" date :: Maybe Day
   case dayFromUser of
     Nothing -> Nothing
     Just validDay -> do
       let differenceInDays = diffDays validDay retrievedDate
       if differenceInDays >= 0 && differenceInDays <= 16
          then return $ Right validDay
          else return $ Left InvalidDate

and this error message

Expected type: IO (Either UserError Day) Actual type: Maybe (Either UserError Day)

How can I fix this?

Upvotes: 0

Views: 315

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

The culprit is Nothing -> Nothing, where the first Nothing is a Maybe Day and the second is a Maybe (Either UserError Day).

You want

Nothing -> return $ Left InvalidDate

(or some other UserError).

Upvotes: 4

Related Questions