4

With:

mailHandler = do 
  name  <- param "name"
  email <- param "email"
  renderSendMail $ forwardMail name email

main = scotty 3000 $ post "/mail" mailHandler

I get the following type error:

    Couldn't match type `IO'
                  with `Web.Scotty.Internal.Types.ActionT T.Text IO'
    Expected type: Web.Scotty.Internal.Types.ActionT T.Text IO ()
      Actual type: IO ()
    In a stmt of a 'do' block: renderSendMail $ forwardMail name email
    In the expression:
      do { name <- param "name";
           email <- param "email";
           renderSendMail $ forwardMail name email }
    In an equation for `mailHandler':
        mailHandler
          = do { name <- param "name";
                 email <- param "email";
                 renderSendMail $ forwardMail name email }
Failed to install server-0.0.1
cabal: Error: some packages failed to install:
server-0.0.1 failed during the building phase. The exception was:
ExitFailure 1

After spending several hours, I still don't see how to resolve this. If I use a function internal to Scotty, no problem, but if I want to handling anything externally I get the error above. How can I get past this Web.Scotty.Internal.Types.ActionT T.Text IO ()

Fresheyeball
  • 29,567
  • 20
  • 102
  • 164

1 Answers1

11

Web.Scotty.Internal.Types.ActionT T.Text IO () is a monad transformer over IO. These usually implement the MonadIO class, so that you can use the liftIO function (from Control.Monad.IO.Class, if it hasn't been imported already) to "lift" IO actions into them:

liftIO . renderSendMail $ forwardMail name email
Ørjan Johansen
  • 18,119
  • 3
  • 43
  • 53
  • If you wouldn't mind, please tell me the name of the cabal package to install to get `Control.Monad.IO.Class`. And even better if you can explain this whole monad transformer concept – Fresheyeball Aug 31 '14 at 04:23
  • ok, I got `transformers` installed and things are alive again, but would still love an explanation of the concept. – Fresheyeball Aug 31 '14 at 04:25
  • @Fresheyeball `scotty` seems to already depend on `transformers`, so I suspect that was redundant? (Since `scotty` defines a transformer itself, it sort of has to.) – Ørjan Johansen Aug 31 '14 at 04:29
  • @Fresheyeball Monad transformers is a somewhat big subject, so I think I'll pass on explaining it all here, but see e.g. [this Wikibook chapter](http://en.wikibooks.org/wiki/Haskell/Monad_transformers). – Ørjan Johansen Aug 31 '14 at 04:38
  • 1
    @Fresheyeball Also this paper: http://www.grabmueller.de/martin/www/pub/Transformers.pdf – danidiaz Aug 31 '14 at 09:50