【问题标题】:Anonymous class with methodMissing带有 methodMissing 的匿名类
【发布时间】:2016-02-11 16:46:42
【问题描述】:

对于我正在编写的 DSL,有时我想返回一个忽略所有调用的虚拟对象。

这行得通:

class Dummy {
    def methodMissing(String name, args) { print "Ignoring: "+name+" "+args }
}

def foo(guardCondition) {

    if (guardCondition)
        return new Dummy()

    // ...
}

foo(true).with { bar('baz') }

但如果我尝试使用匿名类,结果会出现类似No signature of method: <filename>.bar() is applicable for argument types: (java.lang.String) values: [baz]的错误

也就是说这不起作用:

    if (guardCondition)
        return new Object() { def methodMissing(String name, args) { print "Ignoring: "+name+" "+args } }

“袋子”方法也不起作用:

    if (guardCondition)
        return [ methodMissing : { name, args -> print "Ignoring: "+name+" "+args } ]

【问题讨论】:

    标签: groovy anonymous-types anonymous-class


    【解决方案1】:

    关于这个有一个未解决的问题:GROOVY-4862。事实上,它在内部类中不起作用,无论是否匿名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2017-08-14
      • 1970-01-01
      相关资源
      最近更新 更多