ginger icon indicating copy to clipboard operation
ginger copied to clipboard

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

Open tdammers opened this issue 7 years ago • 1 comments

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

tdammers avatar Jun 10 '18 09:06 tdammers

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

wchresta avatar Jun 10 '18 12:06 wchresta