Open wchresta opened 6 years ago
As discussed, potential solutions:
CallE
was invoked (function call, filter, test, macro invocation), and then hand that information to functions, which can then change their behavior accordingly. This would allow us to write a gfnLower
that acts as an "all-lowercase" test when invoked through is
, and as a "make lowercase" function otherwise. This is terrible.is
that mangles the test name to prefer some kind of specially-named builtin (ideally one that you cannot produce from within the template). E.g., foobar is lower
would then parse into CallE (VarE "is-lower") [(Nothing, VarE "foobar")]
(note the is-
prefix on the variable name). This would work, but I'm really reluctant to add this kind of hard-to-explain magic.So all the actual solutions kind of suck.
Additionally, I can't really think of a reasonable use case where you'd say "oh, hmm, I need to check whether everything in this string is lowercase" - the only situations I can think of are ones where you'd want to actually convert the string to lowercase anyway.
So I'm inclined to say that we should just accept this incompatibility (is lower
doing something somewhat unintuitive, and deviating from jinja), document it as such, and call it a day.
On further thought, I believe option 3 might not be so bad after all.
The thing is that there are more tests in Jinja2, and many of them do make sense in Ginger, like for example the is number
test.
So here's how I think it should work:
is
syntax construct should take care of prepending is_
to the function name, thus foo is bar
should be desugared to is_bar(foo)
. This also implies that is
syntax is going to be a lot more restrictive than filter or function call syntax: only literal function names are allowed for the filter name, so unlike filters or functions, you cannot use arbitrary expressions as tests. This isn't going to be a huge problem, because Jinja2 has the same restriction. It also means that the is
construct should have very high precedence, i.e., foo + bar is even
should parse as foo + (bar is even)
, not (foo + bar) is even
.is_
prefix added. This also exposes them as plain old functions, true to the "tests and filters and functions are all the same thing" paradigm, but it avoids name clashes between things like lower
and is_lower
.is_
prefix is added, it will work just fine.
ginger
decided that there is no difference between functions, tests and filters. Unfortunately, there is a clash for the tests and filterslower
andupper
:In jinja2:
|upper
returns an uppercased string which is always truthyis upper
returns true only if the string is already uppercasedThe same goes for
lower
respectivelyThis blocks #18