groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henrik Martin <>
Subject Emulating method overloading with closures?
Date Wed, 15 Apr 2015 00:19:49 GMT
Hi. I have a Groovy method that is overloaded, and I'm trying to achieve 
something similar with closures. The methods look something like this:

int foo(String args, boolean x, boolean y) { ... }
int foo(String args, boolean x, boolean y, boolean z) { ... }

The first method basically just forwards to the overloaded version, 
passing a 'z' argument with a default value. Is there any way I can 
convert these two methods to two closures in the same class in a similar 
fashion as method overloading? The problem I run into seems to be that 
closures are object instances, so I can't really have two closures with 
the same name in the same class. Maybe using closures isn't appropriate 
in this case? What would be a "Groovier way" of doing this? The reason I 
tried the closure approach is the "method as an object instance" 
paradigm fits pretty well with the particular project I'm working on. 
But I'm sure there's a better way of doing this...


View raw message