简体   繁体   中英

Caught: groovy.lang.MissingMethodException: No signature of method

Simple code: ClosuresSyntax.groovy

{ -> item++ }
{ item -> item++ }

it cause an exception:

Caught: groovy.lang.MissingMethodException: No signature of method:  
       com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure1.call()      is applicable for argument types:
(com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure2) values: 
    [com.lucaslee.groovy.syntax.ClosuresSyntax$_run_closure2@1534f01b]     

Your code is the same as (note the parentheses):

{ -> item++}({ item -> item++})

The definition of both closures is fully correct. The problem is that in fact the first closure is run with the second one passed as an argument. This is exactly the same:

{ it -> it() } { println 1 }

Since you can't invoke ++ on a Closure object MissingMethodException is thrown. This will work correctly eg:

{ item -> item()++ }{ 1 }

A closure {1} is passed as an argument, invoked () and a result is incremented ++ .

To verify that closures definitions are correct, run:

def a = { -> item++ }
def b = { item -> item++ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM