带有{…this@foo=…}:groovy.lang.MissingMethodException:方法MyClass.setFoo()的签名不适用于参数类型

带有{…this@foo=…}:groovy.lang.MissingMethodException:方法MyClass.setFoo()的签名不适用于参数类型,groovy,Groovy,(使用Groovy 2.4.11) 以下伪修改代码: enum EnumClass { a, b } class Some { Foo foo Some() { EnumClass.with{ this.@foo = new Foo( a ) } } Some setFoo( String _foo ) { ... } } 调用类似于new Some(),并引发以下运行时异常: groovy.lang.MissingMethodException:

(使用
Groovy 2.4.11
) 以下伪修改代码:

enum EnumClass { a, b }

class Some {

   Foo foo

   Some() {
     EnumClass.with{ this.@foo = new Foo( a ) }
   }

   Some  setFoo( String _foo ) {  ... }
}
调用类似于
new Some()
,并引发以下运行时异常:

groovy.lang.MissingMethodException:没有方法MyClass.setFoo()的签名适用于参数类型:(Foo)值:[Foo$12345]`

它看起来好像编译器认为会有一些
this.foo=…
而不是
这个。@foo=…
:-(
(据我所知,这不应该发生,而且似乎是一些bug)

解决方法:这样编写(在
之外使用
-closure)很有效

enum EnumClass { a, b }

class Some {

   Foo foo

   Some() {
     //EnumClass.with{ this.@foo = new Foo( a ) }  // throws exception
     this.@foo = new Foo( EnumClass.a )            // works
   }

   Some  setFoo( String _foo ) {  ... }
}