quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.63k stars 2.64k forks source link

Qute: support boolean/ternary expressions everywhere #13106

Open FroMage opened 3 years ago

FroMage commented 3 years ago

ATM it appears that boolean expressions and comparison operators are only available in #if tags. I tried using them in the {bool ? expr1 : expr2} ternary and they didn't appear to work.

If it's at all possible with the internals, we should make sure that every expression passed to a any tag is evaluated with the same grammar, like in programming languages we have statements and expressions, and every expression can occur in every context.

The way this is usually done is by having an evaluate/parseExpression(Expression) which then switches/delegates to the next operator precedence, say evaluate/parseParenthesis(ParenExpression) or evaluate/parseComparison(ComparisonExpression) until we get to the bottom of the expression.

And then every tag would use the same expresion evaluation, with different scopes naturally.

quarkusbot commented 3 years ago

/cc @mkouba

mkouba commented 3 years ago

Yes, {bool ? expr1 : expr2} works but you cannot use nested operators ATM.

The value expressions are currently designed a little bit differently, i.e. not as a full featured statements or expressions. That said, it should be fairly easy to implement simple operators (such as foo > 1 ? 'big' : 'small') but not complex expression. We could definitely try to revisit our design and API.

FroMage commented 3 years ago

Yeah, it would be nice to make value expressions full-fledged expressions, that's much more intuitive and useful.

mkouba commented 2 years ago

FTR, the operators are actually implemented as "virtual methods" that can be used with infix notation in output expressions, so bool ? expr1 : expr2 can be translated to bool.ifTruthy(expr1).or(expr2) and this form works everywhere.

FroMage commented 2 years ago

Sure, but that's a workaround.

mkouba commented 1 year ago

For the record - https://github.com/quarkusio/quarkus/pull/29154 makes it possible to use the infix notation in section params with parentheses, e.g. you can do something like {#let foo = (val ? 1 : 2)}.