views:

109

answers:

1

Hi,

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

+1  A: 

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.

Nathan Hughes
Can you explain (maybe give an example) of what you mean by"you need to provide a doCall() method to specify the parameters in order to call the closure in the short form"
Don