Haskell’s Text.JSON library uses an abstract data type called Result, it’s basically their form of Maybe, but instead of Nothing, there’s Error String. Anywho, I need to use liftIO to convert a function call returning an IO thing to a Result thing inside of my implementation of JSON.readJSON. I’m new to monad transformers and can’t seem to implement liftIO for Result (I keep trying to construct the infinite type, according to ghci).
Any ideas?
Many thanks
EDIT
Sorry it’s taken me so long to elaborate! I appreciate your help guys.
readJSON (JSObject obj) = do text <- getVal obj "text"
user <- getVal obj "from_user"
iden <- getVal obj "id_str"
url <- (do if (length.extractURLs) text == 0
then return ""
else return $ head $ extractURLs text)
title <- liftIO (getSiteTitle url)
return $
Tweet
NewsStory {
title = "Twitter",
desc = text,
url = url,
metric = 0,
sourceURL = "twitter.com/" ++ user ++ "/status/" ++ iden
}
So the last line before return uses getSiteTitle to parse a website at that url for its title. However, that function returns a type of IO String and the compiler tells me it wants the it to be Result. Is this impossible?
Thanks again!
EDIT2
I’ve decided to eliminate the title from my data type and get it later on when inside an IO monad. Thanks for everyone’s help! I’ve certainly learned from this issue.
You can’t use IO inside of readJSON (without resorting to unsafePerformIO). liftIO is used when you have a stack of monad transformers with IO at bottom. Perhaps if you give more concrete information of what you are trying to achieve you’ll be able to get a more useful answer 🙂