[18][guarded pattern] conditional-and query - spec clarification
Manoj Palat
manoj.palat at in.ibm.com
Mon Mar 7 07:08:11 UTC 2022
Hi,
Given,
public void bar(Object o) {
int i = switch(o) {
case String a && o != null ? true : false -> 1;//ecj flags syntax error here
default -> 1;
};
}
ECJ(eclipse compiler for Java) flags a syntax error on the guarded pattern. However, javac accepts.
Ecj translates this into:
case ((String a) && (o != null)) ? true : false
and flags an error instead of
case ((String a) && ((o != null) ? true : false))
And I think the ecj is correct in flagging the error due to:
From https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html we see that Conditional-And Operator “&&” has higher operator precedence than the Conditional Operator “?:” . From https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.23, we see that “The conditional-and operator is syntactically left-associative (it groups left-to-right).”
Also, I don't see any mention of the precedence changes in spec 420 [latest at https://cr.openjdk.java.net/~gbierman/jep420/latest]
A more detailed reasoning (the above cut verbatim from here) captured at https://bugs.eclipse.org/bugs/show_bug.cgi?id=578856#c1
Is there something which I am missing in this reasoning here?
Regards,
Manoj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220307/f2bcb123/attachment.htm>
More information about the amber-spec-experts
mailing list