开发者

Invoking Groovy closures

开发者 https://www.devze.com 2022-12-15 20:02 出处:网络
If I define a closure in Groovy def c = {println \"foo\"} I can invoke it using either c() or c.call() AFAIK, these two are identical. However, I recently discovered a third way

If I define a closure in Groovy

def c = {println "foo"}

I can invoke it using either

c()

or

c.call()

AFAIK, these two are identical. However, I recently discovered a third way

c.doCall()

Are there any differences between call() and开发者_如何学编程 doCall()

Thanks, Don


The doCall method is what gets invoked when you call c() or c.call().

I found an example that claimed it's used to call the closure from inside itself, but that seems to work with call() too.

The documentation says you need to provide a doCall() method to specify the parameters in order to call the closure in the short form (without explicitly using call()). But I don't know how exactly they expect that to work.

Here's an explanation of call vs. doCall.

0

精彩评论

暂无评论...
验证码 换一张
取 消