Groovy: Named Parameter Constructors

I was very cool what can be done:

class Foo { String name } def foo = new Foo(name:"Test") 

But this only works when the file name matches the class name. If I have a file with a bunch of classes like:

 class AllClassesInOneFile { class Bar {} class Foo { String name } } def foo = new Foo(name:"Test") 

Now it no longer works. I get java.lang.IllegalArgumentException: wrong number of arguments

I wonder if it is possible to call the argument style of the parameter name using scripts and nested classes.

Hello

+6
source share
1 answer

Groovy seems to need an explicit reference to an instance of an external class:

 class Baz { class Bar {} class Foo { String name } } def baz = new Baz() def f = new Baz.Foo(baz, [name: "john doe"]) assert f.name == "john doe" 
+6
source

All Articles