13

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.

Community
  • 1
  • 1
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • +1 It's a very interesting question, but largely of academic interest. In practice, this would be a highly unusual way to write a return statement – Dónal May 01 '12 at 23:27
  • True, for the most part. I'd say there's some validity for `a`, `b`, and `c` sufficiently ugly. – Travis Webb May 01 '12 at 23:35

3 Answers3

25

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

Groovy doesn't seem to require semicolons, so I think your code is being intepreted like:

return a;
    && b;
    && c;

From the documentation:

Groovy uses a similar syntax to Java although in Groovy semicolons are optional.

This saves a little typing but also makes code look much cleaner (surprisingly so for such a minor change). So normally if one statement is on each line you can ommit semicolons altogether - though its no problem to use them if you want to. If you want to put multiple statements on a line use a semicolon to separate the statements.

Community
  • 1
  • 1
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
2

you can do almost all java in groovy except you look at the following.

http://groovy.codehaus.org/Differences+from+Java

if you want to do straight java then you can do it in a *.java class and drop it into the src folder.

nate_weldon
  • 2,289
  • 1
  • 26
  • 32
  • I've seen this page, and my issue isn't addressed there. – Travis Webb Apr 17 '12 at 20:12
  • 2
    it kind of dances around it Things to be aware of Semicolons are optional. Use them if you like (though you must use them to put several statements on one line). – nate_weldon Apr 17 '12 at 20:14
  • Yes I already know that semicolons are optional... this isn't helpful. And using a semi-colon here doesn't even solve the issue. – Travis Webb Apr 17 '12 at 20:16
  • 1
    the basic problem is the groovy compiler does not recognize that the line is continued on the next line you need something to tell the compiler that the line is continued – nate_weldon Apr 17 '12 at 20:32