You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2019/10/15 15:49:00 UTC

[jira] [Created] (GROOVY-9278) Java compatibility: consider supporting local class definitions

Eric Milles created GROOVY-9278:
-----------------------------------

             Summary: Java compatibility: consider supporting local class definitions
                 Key: GROOVY-9278
                 URL: https://issues.apache.org/jira/browse/GROOVY-9278
             Project: Groovy
          Issue Type: New Feature
            Reporter: Eric Milles


Consider the following Java code:
{code:java}
class Main {
  public static void main(String[] args) {
    class Local implements Runnable { // allows modifiers "final" and "abstract" only!
      @Override public void run() {
        System.out.println("works");
      }
    }
    new Main().meth(new Local());
  }
  void meth(Runnable runnable) {
    runnable.run();
  }
}
{code}

I was not aware this was supported until encountering it in another codebase.  Typically I would use an anonymous inner class in similar situations.  For increased cross-compatibility, Groovy could implement support for locally defined classes.  There is probably some Java spec that defines their characteristics.

At the moment groovyc emits "Class definition not expected here. Please define the class at an appropriate place or perhaps try using a block/Closure instead." for this code.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)