hamlet templates and deconstructing data in $forall : why it doesn't bring deconstructed members in scope?

StackOverflow https://stackoverflow.com/questions/19143820

  •  30-06-2022
  •  | 
  •  

Question

In Yesod, using whamletFile function in a route handler, I have issue deconstructing records in the $forall construct.

I have this data record:

data Foo a = Foo (a, Int, Int)

in hamlet template file, I itterate over an instance of [Foo] and try to use deconstructing syntax:

$forall (Foo (a, b, c)) <- foos
  <li>#{a}

it fails with this message Not in scope: 'a' while compiling

while this won't fail and would process the forall construct appropriately:

$forall (Foo (a, b, c)) <- foos
  <li>nothing special

Any idea why using deconstructing syntax would fail to bring the items in scope?

Was it helpful?

Solution

Your deconstruction syntax is wrong. Try this:

$forall Foo (a, b, c) <- foos
    <li>#{a}

Also, your data declaration actually declares a type with one field - a tuple. If you want to declare a type with 3 fields your syntax should be different:

data Foo a = Foo a Int Int

It deconstructs more naturally:

$forall Foo a b c <- foos
    <li>#{a}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top