Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty do block yesod after creating new handler

Tags:

haskell

yesod

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
like image 364
Sandro_V Avatar asked Aug 31 '26 00:08

Sandro_V


1 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.

like image 194
K. A. Buhr Avatar answered Sep 02 '26 17:09

K. A. Buhr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!