Groovy vs. Java syntax discrepancy

groovy, java, syntax

Solution

The problem is that Groovy doesn't require explicit line terminators - and `return a` looks like a valid statement on its own. You could use:

return a &&
       b &&
       c;

Or use a line continuation:

return a \
    && b \
    && c;

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.

Problem

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.

Original source

Related problems