tdammers / ginger

A Haskell implementation of the Jinja template language.
MIT License
77 stars 13 forks source link

Do we want `ToGVal` instances for string types to parse strings as numbers? #25

Open tdammers opened 6 years ago

tdammers commented 6 years ago

In Jinja2, the following is a type error:

{{ "2" + 2 }}

But in Ginger, we currently have a ToGVal instance for Text (and other string types) that tries to parse the string into a Scientific, and if that succeeds, exposes it in the asNumber field, which means that in Ginger, the above template prints "4".

So the question here is whether it is more important to keep the current convenience of being able to use numeric strings as numbers, or to be more compatible with jinja2.

I am leaning slightly towards keeping the current behavior, because the problem is essentially just being too lenient (accepting templates that Jinja2 rejects), but everything that works fine on Jinja2 will also work fine on Ginger (i.e. we accept templates that Jinja2 accepts).

wchresta commented 6 years ago

I think it comes down to personal preference. Myself, I'm not a fan of implicit string to number conversions as I believe the time that is lost by hunting down weird unexpected effects is greater than the time saved by not having to type |int.

There should also be a decision what do do with strings of the form "23e2" and "1e0".