[web-devel] A few questions about Yesod

Michael Snoyman michael at snoyman.com
Thu Dec 30 14:46:35 CET 2010

On Thu, Dec 30, 2010 at 3:31 PM, Iustin Pop <iustin at google.com> wrote:
> On Thu, Dec 30, 2010 at 03:09:16PM +0200, Michael Snoyman wrote:
>> Very good questions, answers below.
>> On Thu, Dec 30, 2010 at 1:01 PM, Iustin Pop <iustin at google.com> wrote:
>> > Hi all,
>> >
>> > I just started looking at Yesod and its associated libs (hamlet, etc.)
>> > and it is very interesting, thanks.
>> >
>> > However, I'm confused by a few things and the docs are not helping, so
>> > please bear my beginner questions.
>> >
>> > First: hamlet uses '.' as function application, instead of the usual
>> > space. How can I then use a qualified name (e.g. Data.List.nub)? If I
>> > use it normally, it errors out on me. Must be something very trivial but
>> > I cannot find a way.
>> Hamlet uses both '.' and space as function application, and therefore
>> qualified names are not supported. I work around this usually by
>> creating an alias for the function I want locally. I know this can be
>> inelegant; if you have any ideas, I'm all ears.
> Yep, that's what I'm using too now, but it becomes cumbersome quickly,
> especially when using records…
> I'm not sure what was the original impetus to use . (too) as function
> application if space is accepted too. I'm thinking reverting that
> decision would make the code look more like regular Haskell.
> A simpler alternative (not sure if easily doable) would be to allow
> escaping of the dot, e.g. $Data\.List\.nub.mylist$; it's ugly, but…

It actually went the other way: period first, and space added by
request. Originally, Hamlet variables were not directly mapped to
Haskell function calls. Instead, it was meant to parallel variable
lookup in common template languages from the object-oriented world.
Another impetus is because of statements like forall and maybe:

$forall allPeople.myFamily person
    %li $person name$

This can also be written as

$forall (allPeople myFamily) person

You could argue that the latter is more legible; my problem with the
space is for cases with more than two functions in the chain. $foo bar
baz$ gets converted to the Haskell code "foo (bar baz)".

I'll admit that this whole situation bothers me as well. Hamlet 0.7 is
currently in the works, and I don't mind introducing some major
changes. I think this issue deserves some attention: what does
everyone else think? Maybe we should start a separate thread to
discuss this issue in particular.

>> > Second: Yesod.Json says “Due to the limited nature of the JSON format,
>> > you can create any valid JSON document you wish using only jsonScalar,
>> > jsonList and jsonMap”. This seems false to me, as jsonScalar only deals
>> > with JSON strins, and not JSON numbers or the constants (true, false,
>> > null). In the meantime I'm using a hack for numbers using jsonRaw, but I
>> > think some more functionality needs to be added to it for non-trivial
>> > usage.
>> Quite correct. I'm working on Yesod 0.7 right now, which will be
>> offloading the JSON work to John Millikin's json-types package which
>> properly supports numbers, booleans and nulls.
> Cool, that sounds good.
>> > And last: I don't understand how runDB and the persistent framework is
>> > supposed to do error handling. Case in point: let's say one tries to
>> > insert a duplicate unique key in a table, which fails as expected "user
>> > error (SQLite3 returned ErrorConstraint while attempting to perform
>> > step.)". The problem is any other use of that particular REST resource,
>> > even with different parameters, results in "user error (SQLite3 returned
>> > ErrorMisuse while attempting to perform bind text.)", and I don't know
>> > if this is due to my usage of the framework or due to something else.
>> > The interesting part is that other resources are working fine.
>> That sounds like a bug in the SQLite backend to be honest. Could you
>> send me a test case that breaks it? It is much appreciated.
> I've just generated a sample site, and added this new resource:
>  /test/#String TestR GET
> with the associated handler:
> getTestR :: String -> Handler RepHtml
> getTestR name = do
>  runDB $ Test.insert (User name Nothing)
>  defaultLayout [$hamlet| Ok |]
> How it behaves:
> /test/a → OK (as expected)
> /test/a → SQLite3 returned ErrorConstraint while attempting to perform
> step (expected)
> /test/a → SQLite3 returned ErrorMisuse while attempting to perform bind
> text (unexpected)
> /test/b → the same message as above, totally unexpected
> This persists until application restart.
> Again, this might be just due to my wrong usage of the library, but I
> haven't found any docs on how DB errors should be handled.
> In use persistent-sqlite-0.3.0, persistent-, yesod-0.6.7.

OK, I can confirm the bug. I've added a test case for it, and it
should be fixed in the 0.4 release. If anyone reading needs the fix
backported to 0.3, let me know.

> thanks a lot for the fast answers!
> iustin

No problem, you happened to catch me at the right time ;).


More information about the web-devel mailing list