GEP-35
|
Abstract
Dynamic Groovy calls a closure with f(x). It cannot call a native lambda that way, although dynamic code
receives native lambdas today: from Function<String, String> f = s → s.trim() under @CompileStatic, from
String::trim, and from Java libraries (Function.identity(), Comparator.comparing(..)). It cannot call a
Groovy coercion proxy that way either, although { it } as Function is made from a closure.
This GEP lets dynamic code call such a value with f(args) or f.call(args), invoking the abstract method of
its functional interface. No other object becomes callable.
Motivation
| Dynamic code | Groovy 6.0.0 | This GEP |
|---|---|---|
|
MissingMethodException |
|
|
MissingMethodException |
|
|
MissingMethodException |
|
a closure, |
|
|
Groovy 6.0.0 already calls the abstract method in one case: a local variable declared with the functional
interface type, as in StringTransform t = { .. } for interface StringTransform { String transform(String s) }.
There t(x) and t.call(x) call transform, converting the argument to the parameter type as a cast does, so
t.call(1) passes '1'. That is unchanged by this GEP, and it is more lenient than the rule below, which
rejects 1 for a String parameter. The table’s values are held in untyped variables, where 6.0.0 fails.
The gap matters more with GEP-34, which makes statically compiled lambdas native at more sites: without this
GEP, dynamic code would lose f() on every lambda it receives from statically compiled code. It also matters
on its own, because the values above already exist in Groovy 6 programs, and a user who writes f(x) for a
Function gets a message about a missing call method.
Proposal
1. Which values
A functional value is:
-
a native lambda or method reference: an instance of a class created by
java.lang.invoke.LambdaMetafactory; -
a Groovy coercion proxy: a
java.lang.reflect.Proxywhose invocation handler is Groovy’s conversion handler, asclosure as Fandmap as Fcreate. A proxy class is shared by every proxy for the same interfaces, so this is checked per instance, not per class.
The method called is the single abstract method of the functional interface the value implements, as Groovy’s
closure coercion defines it. Arguments are converted as for any dynamic call; if they do not fit, the call
fails with MissingMethodException as before. So f.call(x) accepts exactly the arguments that a dynamic call of
the abstract method itself accepts, and no others.
The check is against the method’s erased signature, since that is all the value carries at run time. For a
non-generic interface such as StringTransform, t.call(1) is a MissingMethodException, as t.transform(1) is.
For a generic one such as Function<String, String>, the method is apply(Object), so f.call(1) reaches the
lambda and fails there with ClassCastException, as f.apply(1) does in Groovy 6 and as a raw call does in Java.
Only the static type can check the type arguments: statically compiled and type-checked code reject f.call(1)
for a Function<String, String>, and a value whose static type is the interface cannot be returned or
assigned as a Closure without a cast.
No other object is a functional value. In particular a List (through Iterable), a Thread or a
Runnable, an InputStream (through AutoCloseable), a Comparable, a Comparator that is not a lambda,
such as Comparator.naturalOrder(), and a user class implementing a functional interface keep throwing
MissingMethodException. A wider rule, "any object implementing exactly one functional interface", made
[1, 2]() return an iterator and stream() close the stream, and was rejected for that reason.
2. When the rule applies
The rule runs after everything the MOP already tries, as a last resort before MissingMethodException:
categories, methodMissing, invokeMethod, a call property, and the outer class of an inner class. So a
class whose methodMissing answers f(x) keeps answering it, and the message of a MissingMethodException is
unchanged for every object that is not a functional value.
The rule applies to call syntax on the value itself: a local variable or a parameter holding it, as f(x) and
f.call(x). It does not apply to obj.f(x) where f is a property of obj
holding a lambda: that call is a method call on obj, and it fails as it does today, while a property holding
a closure keeps working as it does today. Extending the rule to properties is an open question.
Statically compiled and type-checked code already call a typed functional value through its interface, since GROOVY-8466 rewrites call syntax on a receiver whose static type is a functional interface. This GEP changes dynamic dispatch only.
3. Performance
The call is bound at the call site by the invokedynamic Selector, with guards that keep the binding to the
receivers it was made for, so a hot f(x) does not take the missing-method path on each call.
| Call | ns per call |
|---|---|
dynamic |
1.04 |
dynamic |
0.90 |
dynamic |
0.98 |
dynamic |
10.8 |
the same call through the meta-class, without the call-site binding |
195 |
4. Realms
Whether the rule depends on the caller’s realm is decided with GEP-31. Until then it applies to every caller.
Compatibility
| Change | Who can notice | Kind |
|---|---|---|
|
code that tells callable values from others by catching MissingMethodException, as in |
behaviour change |
Nothing else becomes callable |
none |
— |
No public API is added. The support class is internal.
Relation to GEP-33 and GEP-34
-
GEP-33 does not need this GEP: its implementing closures are closures, and dynamic code calls them as it calls any closure.
-
GEP-34 needs this GEP, so that dynamic code keeps
f()on the lambdas GEP-34 makes native. -
Version 1 of GEP-34 proposed closure literals at declared targets as implementing closures, partly so that dynamic
s()would work onSupplier<String> s = { 'x' }. This GEP makes that call work on the proxy instead, without the overload,grepandswitchchanges, and GEP-34 version 2 defers that part.
This GEP can land before either of them.
Work items
-
The rule of sections 1 and 2 in the meta-class, after the existing MOP fallbacks.
-
The call-site binding in
Selector, with guards that are correct for a proxy class shared by proxies with different handlers. -
Tests: every value kind of section 1, the excluded objects,
methodMissingandinvokeMethodprecedence, properties, wrong arity, and the indy and non-indy paths. -
The JMH benchmark in
subprojects/performance. -
Documentation: call syntax in the closures and the functional-interfaces chapters; a Groovy 7 compatibility entry.
Alternatives considered
- Call syntax on every functional-interface instance
-
Simplest to state, but makes ordinary objects callable (section 1). Rejected.
- Call syntax on every lambda, including through properties
-
Makes
obj.f(x)call a lambda-valued property, as it calls a closure-valued one, but changes which of a class’s members answers a call. Left open. - Convert the value to a closure first
-
Allocates per call and loses identity. Rejected.
Open questions
-
Should
obj.f(x)call a propertyfholding a functional value, as it does for a closure, whenobjhas no methodfand nomethodMissing? The same rule decides a map entry: withMap<String, Function<String, String>> commands,commands.shout('hi')is a MissingMethodException whilecommands.shout.call('hi')works, and the map-of-commands idiom is common with closures. -
Should this land in 6.x? It changes only calls that fail today, but code that relies on the failure exists.
References and useful links
Update history
- 1 (2026-11-26)
-
Split from section 4 of GEP-34 version 1, with the implementation’s rules for properties and the measured call costs.
- 2 (2026-11-27)
-
Section 1 states which arguments
f.call(x)accepts, and that a generic interface is checked against its erased signature. Motivation notes the typed-local case where Groovy 6.0.0 already calls the abstract method.