[web-devel] problem with working from a Yesod example, ajax.hs <reposted from Haskell-Cafe and Haskell-Beginnners>

Max Cantor mxcantor at gmail.com
Tue Mar 29 06:20:25 CEST 2011


> 
> [Original]
> 
>> data Page = Page
>>  { pageName :: String
>>  , pageSlug :: String
>>  , pageContent :: String     ******** I'm going to change this **********
>>  }
> 
> [Changed]
> 
>> data Page = Page
>>      { pageTitle :: String
>>      , pageSlug :: String -- ^ used in the URL
>>      , pageContent :: IO String           ******** This is the change *******
>>      }
> 
> 
> Here's where I run into trouble
> 
> [Original]
>>  json page = jsonMap
>>      [ ("name", jsonScalar $ pageName page)
>>      , ("content", jsonScalar $ pageContent page)   ******** I'm going to change this ********
>>      ]
> 
> 
> 
> [My changes]
> 
>>  json page = jsonMap
>>      [ ("name", jsonScalar $ Main.pageTitle page)
>>      , ("content", jsonScalar $ liftIO $ pageContent page) ******* This is the change ***********
>>      ]
> 

Sorry, I don't have time to go through the whole email now, but the above snippets are a bad code smell in haskell.  if you change the type of pageContent to IO String then you cannot access it in pure code as you are trying to do.  liftIO isn't :: IO a -> a.  

You probably want something like this:
do 
  ...
  content <- liftIO $ pageContent page
  json page = jsonMap
    [ ("name", jsonScalar $ Main.pageTitle page)
    , ("content", jsonScalar content)      
    ]

FYI, this particular issue isn't particular to webdevel or yesod and you might get faster help in haskell-beginners or haskell-cafe.

Also, I think you'll get a better response if instead of one giant email you try to keep each email short with only one or two issues per message.  But, best not to just blast 10 emails at once either, wait for a response before sending the next one :)

Max




More information about the web-devel mailing list