Haskell Either in main

367 views Asked by At

I am having trouble to use my function that returns an Either monad in my main :: IO() function. I am able to run my code without using Either, but I now use Either to handle errors. I have the following code:

parse :: Parser a -> String -> Either TypeRep a
parseFile :: String -> Either TypeRep Game

main :: IO()
main = do 
  content <- readFile "file.txt"
  let info = parseFile content
  case info of
    Left e -> error $ show e
    Right game -> let level = makeGame game
               Gloss.play ... level ... 

So my code was able to do Gloss.play and so on if my parse-function just returned a. How can I handle Either in the main with IO.

1

There are 1 answers

0
K. A. Buhr On

You're close. In your Right game -> case, you want to introduce another do-block, so something like:

main :: IO()
main = do 
  content <- readFile "file.txt"
  let info = parseFile content
  case info of
    Left e -> error $ show e
    Right game -> do
      let level = makeGame game
      Gloss.play ... level ...