Community
Participate
Working Groups
Hi, I got this very surprising behaviour of xtend with the java generated code: class BugsIfElse { def test() { val r = if (true) { 1 } else { 2 }.m } def m(int x) { x + 5 } } generates to: @SuppressWarnings("all") public class BugsIfElse { public void test() { int _xifexpression = (int) 0; if (true) { _xifexpression = 1; } else { _xifexpression = this.m( 2); } final int r = _xifexpression; } public int m(final int x) { return (x + 5); } } As you can see, the call to m is applied to the content of the else block, and not to the result of the complete if/else block.
you call m on the block expression. That's how the precedence is defined. You need to use parenthesis if you want to apply m on the if expression's result. def test() { val r = (if (true) { 1 } else { 2 }).m }
Hi, I know that yes, but the "bug" is that it is very surprising and can lead to errors not seen by the user. It doesn't seem natural at all! Maybe we could add a warning about it or prevent such situation from happening?
Maybe we could optionally validate MemberFeatureCalls in else-branches that have a block expression as their target? That validation could be refined if more of these irritations come up. Reopened to discuss that issue.