0

The question, Is Groovy syntax an exact superset of Java syntax? has not been updated in nearly a decade.

My understanding is that Groovy 3 has closed the remaining gaps with Java syntax, to become a pure superset. I believe this is what Wikipedia means when it refers to Groovy as,

Java-syntax-compatible

Is this correct, or is there still Java (11) syntax that is invalid Groovy?

jaco0646
  • 15,303
  • 7
  • 59
  • 83

1 Answers1

1

I found an authoritative page: http://groovy-lang.org/differences.html, however, most of the differences are behavioral (runtime) rather than syntactic.

There may be only two syntax gaps remaining.

  1. Groovy 3 does not support Java's shorthand array initialization.
    int[] array = {1, 2, 3};
  2. Groovy 3 does not support Java's String literal dollar character.
    String dollar = "$"

Groovy 3 includes five additional keywords; however, this may not be a problem for compiling Java code.

Groovy is less stringent than Java in that it allows some keywords to appear in places that would be illegal in Java

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • `"$"` are 2 different constructs in Groovy and java – injecteer Aug 09 '20 at 18:34
  • Yes. Per the linked document, "_As double-quoted string literals are interpreted as GString values, Groovy may fail with compile error... if a class with String literal containing a dollar character is compiled with Groovy and Java compiler._" I don't think there is any way to close this syntax gap. I would have liked to see Groovy 3 handle the shorthand array initialization by keying off of the type declaration; but I don't see a good way to handle literal dollar signs from Java code. – jaco0646 Aug 09 '20 at 19:18
  • why should Groovy blindly mimic all Javas syntactic garbage? – injecteer Aug 09 '20 at 19:23
  • 1
    Mainly because it can, with relative ease. It's great for marketing. One of the primary features that Groovy promotes is "smooth Java integration". Admittedly, that is more about library integration; but the pool of potential Groovy users is full of people familiar with Java syntax. Lowering their barrier to entry is valuable... and I believe it was also a goal of version 3. – jaco0646 Aug 09 '20 at 19:47
  • well, Groovy IS a better java, comparing to all other JVM languages. In Groovy simple copiing&pasting java code would work in 95% (used to be 80% in Groovy 2). I don't think that it makes sense to lower the barrier that much, and would be another 80-20 principle implementation. Also "smooth Java integration" is not really the main reason for people to switch to Groovy, unfortunately... – injecteer Aug 09 '20 at 19:59
  • The 80% in Groovy 2 becoming 95% in Groovy 3 is what motivated me to ask this question. I agree that 100% parity is not necessarily the goal; but I'm curious exactly what that small remaining percentage includes, particularly with respect to syntax. – jaco0646 Aug 09 '20 at 20:24