Skip to end of metadata
Go to start of metadata

At present, there is a lot of weirdness with how bare code and embedded classes are handled in Groovy files. A recent example from Jim Menard on the user mailing list:

in jimm/SimpleApp.groovy:

This code won't compile. Groovy attempts to generate SmallApp.class for the embedded class, and another SmallApp.class for the script itself. This is rather counter-intuitive.

I think the rules have to be more subtle. If there is only one class in the script, then that should be the only class generated. Bare code should go into static main() or, more intuitively (I think), into static initializers for the class. If there is more than one class in the script, the script gets its name, and the declared classes become inner classes.

I think static initializers are more intuitive for bare code than static main() because you can have many of them, so there is no conflict with user code (ie. other static initializers or an explicit static main()). My feeling is that we should check, and if there is no static main() declared, we supply an empty one so that Java doesn't complain.

I think these policies will most closely match expectations, while providing the fewest conflicts to be resolved.

– Chris Poirier

  • No labels

1 Comment

  1. Static initialisers causes problems if the class is serialised. If it is then deserialised in another JVM the bare code is executed. Also doesn't the script class have to subclass Script?

    The idea that declared classes become inner classes of the script class seems attractive to me.

    What's the deal with the script Binding? I presume that the code in the methods in embedded classes must resolve against the Binding.