Invoking Groovy closures

Viewed 9517

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 Answers

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.

Related