Empty do block yesod after creating new handler

Viewed 212

I try to create a new route in yesod / haskell with a handler called state, but I get the error empty 'do' block

The steps to reproduce are the following:

  1. Create new yesod application: stack new haskellYesod yesodweb/simple
  2. yesod add-handler for adding a new handler with the params:
    • Name of route: State
    • Route Pattern: /state/
    • Methods: GET
  3. Add following code in src/Handler/State.hs
module Handler.State where

import Import

getStateR :: Handler Html
getStateR = do
    defaultLayout $ do
        $(widgetFile "bla")

  1. Create simple HTML site templates/bla.hamlet:
<h1> BLA!
  1. Start server with stack exec yesod devel

After that I get the error:

src/Handler/State.hs:7:21: error: Empty 'do' block
  |
7 |     defaultLayout $ do
2 Answers

What's happening is that without the TemplateHaskell extension, the syntax $(...) isn't recognized as intended. Instead, it's parsed as a do-block followed by the operator $ and then the expression (...), as if you'd written:

getStateR = do
    defaultLayout $ (do $ widgetFile "bla")
                     ^^ empty do-block

You'd see the same problem with the standalone program:

main = do
    $(thiswontwork)

which is parsed as main = do $ thiswontwork and generates an Empty 'do' block message, too.

Adding {-# LANGUAGE TemplateHaskell #-} to the top of the State.hs file is enough to fix the problem.

The problem was missing extensions. After adding the following, the error disappeared:

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
Related