When compared to the majority of programming languages available, HTL's expression language can lead to some confusing bugs. Consider the example:
<sly data-sly-test.combinedVars="${var1 || var2 && var3}"/>
When provided with the following inputs:
var1: true
var2: false
var3: false
one could reasonably expect the value of combinedVars
to be true
because of the value of var1
. This is because most languages give higher precedence to the &&
operator, than ||
.
In HTL, combinedVars
is false
because both &&
and ||
are the same precedence. This means the statement above is evaluated as if it were written:
<sly data-sly-test.combinedVars="${(var1 || var2) && var3}"/>
This interpretation is counter-intuitive to any programming language I have ever used. Though this may be confirmation bias showing through, but given that this was not raised in an issue, as far as I can tell, means very few other people have encountered this issue. If that is true (or close enough) maybe the EL grammar can be changed to separate out this fact, to give &&
a higher precedence.
I am no grammar writing expert, but if my memory serves me well, this would be:
exprNode = orBinaryOp , '?' , orBinaryOp , ws , ':' , ws , orBinaryOp
| orBinaryOp ;
orBinaryOp = andBinaryOp , '||' , andBinaryOp
| andBinaryOp ;
andBinaryOp = factor , '&&' , factor
| factor ;
instead of the original
exprNode = binaryOp , '?' , binaryOp , ws , ':' , ws , binaryOp
| binaryOp ;
binaryOp = factor { , operator , factor} ;
operator = '&&'
| '||' ;
The rest of the grammar remains unchanged, and the grammar continues to be LL. This would have impact on existing code, but I think it would be reasonable to make the change to give developers a more natural feel for HTL EL.
Feedback is appreciated.