In Java I can do this:
return a
&& b
&& c;
In Groovy, it returns a compile error: unexpected token: &&. It also occurs if I omit the return keyword in Groovy. However if I wrap the statement in parentheses, it works fine.
In all the Groovy resources I’ve read, I’ve been told that I should be able to write “straight Java” wherever I want. Is this a bug? If not, what is the reason for this design decision?
I looked here, but did not find this issue listed. I understand that there are some things that cannot be inherited from Java, but this wouldn’t seem like one of those things.
The problem is that Groovy doesn’t require explicit line terminators – and
return alooks like a valid statement on its own. You could use:Or use a line continuation:
It’s not true that all Java is valid Groovy. While most Java syntax is covered, occasionally a feature of Groovy will have an impact on valid Java.