我看到一个新的Groovy特性实现的一个相当奇怪的事情。我有一个(单个方法接口)类,它实现了一个call()
方法,所以我可以像调用一个闭包那样调用它唯一的方法:instance()
而不是必须做instance.call()
。但是,只要我使用破坏的特征引入这样的引用(Callable
)。任何能够提供暗示的人,如果这是预期的,或者是否有解决办法?
class MethodCallSpec extends Specification {
def "call a callable"() {
expect:
def thing = new Callable()
"hello" == thing.call("hello")
}
def "call a callable like a closure"() {
expect:
def thing = new Callable()
"hello" == thing("hello")
}
def "call with callable"() {
expect:
def thing = new SomethingWithCallable()
"hello" == thing.doSomething("hello")
}
}
class Callable {
Object call(def message) {
message
}
}
trait WithCallable {
Callable callable = new Callable();
}
class SomethingWithCallable implements WithCallable {
def doSomething(def message) {
callable(message) // this breaks unless written as callable.call(message)
}
}