Sorry if I might be teaching people to suck eggs, but you can discover the 
functional method of a functional interface through reflection. It’s a bit of a 
lookup but presumably in groovy the results of such lookups do get cached...

I use (reassembled from something that’s a bit more broken up than this):

Optional.of(Function.class)
    .filter(Class::isInterface)
    .filter(c -> c.isAnnotationPresent(FunctionalInterface.class))
    .stream()
    .flatMap(c -> Arrays.stream(c.getMethods()))
    .filter(Predicate.not(Method::isDefault)
        .and(m -> !Modifier.isStatic(m.getModifiers())))
    .findFirst()
    .map(Method::getName);

<< Optional[public abstract java.lang.Object 
java.util.function.Function.apply(java.lang.Object)]

Modify to allow any interface as long as it has only one abstract method by, 
instead of the .findFirst(), go to a list or array and only use it if it’s 
exactly one entry long.

-- 
Rachel Greenham
rac...@merus.eu



> On 29 Apr 2021, at 12:46, Christopher Smith <chry...@gmail.com> wrote:
> 
> That option is not available when using, for example, 
> java.util.function.Function.
> 
> On Thu, Apr 29, 2021, 03:34 Angelo Schneider <angelo.schnei...@oomentor.de> 
> wrote:
> Is that not already covered by the call() - method?
> I mean the option to declare a method called `Object call(args)´
> Best Regards
> Angelo
> 
> ---
> Angelo Schneider
> angelo.schnei...@oomentor.de
> +49 172 9873893
> 
> > Am 29.04.2021 um 02:47 schrieb Christopher Smith <chrylis+gro...@gmail.com>:
> > 
> > It would be convenient to be able to use the convention of "use
> > parentheses on a function-like object" with functional interfaces; for
> > example, if a variable is declared as type Function, to have
> > `myVar(3)` run `myVar.apply(3)`. Is there any chance this would be
> > practical, or would its semantics be limited sufficiently by the
> > default-dynamic nature of Groovy to not add value?
> > 
> > (I have in mind Groovy's tendency to do runtime switcheroos with
> > anything that implements Map as a counterexample, but that may be
> > beside the point.)
> 

Reply via email to