GEP-33
|
Abstract
Under @CompileStatic, a closure literal passed with list.add { .. } to a List<Supplier<String>> is stored
as a plain Closure. The type checker accepted the call, but the erasure of E is Object, so nothing
converts the closure, and a statically compiled list[0].get() fails with ClassCastException. Typed lambdas
passed the same way behave identically, and method references were rejected.
This GEP fixes that mainly by adding to what is stored. A closure literal or lambda written for a
type-parameter slot that the call binds to a functional interface is compiled as a closure class that also
implements the interface. The value is still a Closure, with call syntax, delegate, curry, isCase,
identity and serialization unchanged, and it is now also an instance of the interface. A method reference for
such a slot is compiled for the interface, including where only the call’s assignment, return, cast or
enclosing call gives its type. Untyped parameters take their types from the interface.
Most changes are additions: code that did not compile now compiles, a value that failed when read through the
interface now works, or there is a new warning. Two are not, and are listed under Compatibility: an
untyped parameter can select a different overload, and @PackedClosures(mode = STRICT) rejects closures it
accepted. Dynamic code is compiled as before, but run-time behaviour changes in
one respect: dynamic code can call the interface’s methods on a closure that statically compiled code stored
(section 2.2).
GEP-34 builds on this to give Groovy Java’s model, where a lambda is the same native object wherever it is written and the target of a call can come from its context. GEP-35 lets dynamic code call native lambdas and coercion proxies. The section Where this leads summarises what the three together give.
Motivation
The bug reports
-
GROOVY-10979: a method reference passed for a parameter declared with a type parameter, such as the
UofStream.reduce, was rejected with "parameter type is not a functional interface", although the call bindsUtoFunction<String, Integer>. -
GROOVY-12561: under static compilation,
list.add { 'x' }on aList<Supplier<String>>stores the closure itself, andlist[0].get()fails with ClassCastException.
Both have the same root: a functional literal flows into a slot whose erasure is not the functional interface the program means.
What Groovy 6.0.0 does
| Site | typed closure | typed lambda | String::trim |
|---|---|---|---|
|
coercion proxy, works |
native lambda, works |
native lambda, works |
|
closure, read through F fails |
native lambda, works |
native lambda, works |
|
closure, read through F fails |
closure, read through F fails |
compile error |
|
closure, read through F fails |
closure, read through F fails |
method closure, read through F fails |
|
closure, read through F fails |
closure, read through F fails |
method closure, read through F fails |
|
n/a |
n/a |
compile error |
untyped ( |
compile error when the body needs the type |
compile error |
n/a |
"Read through F fails" means a statically compiled read such as list[0].get() throws ClassCastException.
Dynamic code reading the same value with call syntax works, because it calls the closure. Groovy 5.1.3 behaves
the same, and 6.0.1 differs only in the method reference arguments of #3359 and in list += x, below.
Work since 6.0.0
| Pull request | What it does | Under this GEP |
|---|---|---|
#3089 (GROOVY-10979), merged on master |
method references for type-parameter slots compiled for the interface; an incubating |
method reference arguments kept. The option and the operand path are removed from master: lambdas take the representation of section 2, and native lambdas are GEP-34 |
#3359, GROOVY_6_0_X |
6.0.1 keeps only the method reference arguments of #3089 |
kept; this GEP is a superset |
#3090 (GROOVY-12561), open |
closure literals for type-parameter slots implement the interface; dynamic code can call the interface’s methods on them |
the core of section 2; extended to lambdas. Needs the wrapper rule of section 2.2, and the |
#3340 (GROOVY-12561), open |
the same for operands and conditional arguments |
kept |
#3338 (GROOVY-12824), open |
closure parameter types from the interface for operands, conditional and named arguments of SAM-typed parameters |
kept |
#3336 (GROOVY-12822), open |
a conditional of lambdas or method references assigned to a functional interface no longer throws GroovyCastException |
kept; an independent bug fix, also a 6.0.x candidate |
#3265 (GROOVY-12727), merged on master |
a type argument a lambda or closure needs is taken from the call’s assignment, return or cast target |
kept |
#3337 (GROOVY-12823), open |
the same for method references: |
kept (section 3); additive, a 6.0.x candidate |
#3339 (GROOVY-12825), open, stacked on #3337 |
a method reference in a generic call that is itself an argument takes its type from the enclosing call’s parameter: |
kept (section 3); additive, a 6.0.x candidate. GEP-34 generalises it and must replace its mechanism rather than add a second one |
#3246 (GROOVY-12714), merged on master and 6.0.1 |
wildcard containment follows JLS 4.5.1 |
unaffected. It rejects a closure added to a |
GROOVY-12748, merged on master and 6.0.1 |
keeps the parameter count of an implicit-parameter closure when its call is checked again |
makes |
#3026 (GROOVY-12492), merged on master |
incubating compile-time adapter instead of a runtime proxy for closures with a declared SAM target |
unaffected |
Terminology
- Functional literal
-
A closure literal, a lambda expression or a method reference expression, written in the source at the position being typed. A method pointer (
x.&m) is not a functional literal; it creates aMethodClosure. - Functional interface
-
An interface with a single abstract method that is not a public method of
Object, as in JLS 9.8. - Type-parameter slot
-
A parameter, operand or property whose declared type is a type variable, such as
EofList.add(E)orTofBox<T>.value. Its erasure isObjector the variable’s first bound, so the bytecode carries no functional interface. - Implementing closure
-
A closure class that also implements a functional interface, whose abstract method calls
doCall. WrittenC+Iin the tables. - Wrapper closure
-
A closure made from another by
andThen,>>,<<,curry,rcurry,ncurry,memoize,trampolineorasWritable.
Design principles
-
Add, do not remove. A value stored at a site that compiled in Groovy 6 keeps every capability it had, and gains the interface. The exceptions are listed under Compatibility. Anything that removes a capability in exchange for Java’s model belongs in GEP-34, where its benefit is argued.
-
Literals only. Only functional literals change. A value that already exists, such as a variable of type
Closure, keeps its identity; converting it needsas F. -
Statically compiled sites only. Only statically compiled code stores implementing closures. Dynamic and
@TypeCheckedcode compile literals as in Groovy 6; the one run-time change is how dynamic code treats an implementing closure it receives (section 2.2). -
No options. The representation does not depend on a system property or build setting.
Proposal
1. Where it applies
Under @CompileStatic, a functional literal is targeted when it is written in one of these positions and
the type checker binds the slot’s type variable to a functional interface F:
-
an argument for a type-parameter slot (
list.add(x),list.set(0, x),map.put(k, x), varargs, constructor arguments); -
the operand of an operator method whose parameter is a type-parameter slot (
list << x); -
a subscript or property assignment whose setter or
putAtparameter is a type-parameter slot (list[0] = x,map['k'] = x,box.value = x,map?['k'] = x), and a named constructor argument (new Box<F>(value: x)); -
a branch of a conditional or elvis expression in any of these positions.
The type variable is bound by the receiver’s type arguments, explicit type arguments, the other arguments as in Groovy 6, and the call’s assignment, return or cast target as GROOVY-12727 already does. For method references only, section 3 adds the enclosing call’s parameter.
A slot is not targeted when the call leaves its type variable as a type variable of an enclosing method or
class, as in <T extends Function<String, String>> void fill(List<T> l) { l.add { .. } }. The bound is not
the type of the value: at run time T may be a subinterface of the bound, so implementing the bound would only
move the failure to the readers of List<Subinterface>. Section 4 warns at such a slot.
list += x is not a targeted site. Since Groovy 6.0.1 it is a compile error, because plus infers
List<Object>, which is not a List<F>; GEP-34’s inference makes it compile.
2. Closure literals and lambdas
2.1 Representation
A targeted closure literal or lambda is compiled as an implementing closure:
-
the closure class declares the interface’s abstract method, and its erasure bridge, with the body a coercion proxy for the closure has: the closure is called with its arguments converted and its exceptions translated as
closure as Fwould call it, so a call through the interface behaves as one through the proxy, at about half the proxy’s cost per call. Where those methods cannot be declared, the method callsdoCalldirectly; -
the stored value is the closure, so call syntax,
delegate,owner,curry,memoize,isCase(switchandgrep),equals, identity incontainsandremove, and serialization behave as in Groovy 6; -
statically compiled reads through the interface (
list[0].get()) work; -
untyped parameters take their types from the interface, so
list.add { it.trim() }andlist.add(s → s.trim())compile on aList<Function<String, String>>.
A lambda at list[0] = λ or box.value = λ is already a native lambda in Groovy 6 and stays one: the rule
applies only where Groovy 6 compiles the lambda as a closure. GEP-34 removes that difference.
2.2 In dynamic code
Dynamic code that receives an implementing closure can call the interface’s methods on it, as statically
compiled code does: its abstract method (f.apply(x)), and the default methods it inherits and does not
override. A method of Closure with the same name wins, as it does for any closure: dynamic f.andThen { .. }
selects Closure#andThen(Closure), because the argument is a closure, and returns a composed closure.
A wrapper closure keeps the closure it wraps as its owner, or holds it in a field. Without a rule, a call of
apply on the wrapper would fall through to the wrapped closure and skip the wrapper’s own behaviour:
f.andThen(up).apply(x) would run only f, and b.curry('x', 'y').apply(p, q) would drop the curried
arguments, with no error. So:
-
a wrapper closure answers the abstract method of the interface its wrapped closure implements by calling itself:
f.andThen(up).apply(' a ')isup(f(' a ')), andb.curry('x').apply('y')isb('x', 'y'); -
the interface’s default methods are not answered by a wrapper, so
g.twice(x)on a composed closure is a MissingMethodException, as in Groovy 6; -
a chain of wrappers is followed to the closure that implements the interface.
The wrapper is not an instance of the interface, so this applies to dynamic calls only; a statically compiled cast of the wrapper to the interface fails, as in Groovy 6.
The interface’s parameter types are checked where the value is used through the interface, and only there:
-
statically compiled and type-checked code see the slot’s type,
F, so a wrong argument tol[0].call(x)or to the interface’s method is a compile error, andl[0]cannot be returned or assigned as aClosurewithout a cast; -
a call through the interface’s method, from any code, converts its arguments as a coercion proxy does, so a wrong argument is a MissingMethodException. For a generic interface the check is against the erased signature, as for a native lambda (GEP-35, section 1);
-
through the
Closureview, which dynamic code has and statically compiled code has after a cast, the value is a closure like any other:call(Object…)takes any arguments, and a wrong one fails in the closure body, with ClassCastException where the parameter type came from the interface.Closure<V>types only the result, as in Groovy 6.
Dynamic code does not store implementing closures. The same source compiled dynamically stores a plain closure,
so f.apply(x) works on a value statically compiled code built and is a MissingMethodException on the value
dynamic code built, where Groovy 6 failed on both.
In dynamic code, a value passed to overloads m(Closure) and m(F) still selects m(Closure), and
collect, switch and grep treat it as the closure it is.
3. Method references
A targeted method reference argument is compiled for the interface, as in Groovy 6.0.1. Its type parameter can also come from:
-
the assignment, return or cast target of the call it is passed to:
Function<String, String> f = id(String::trim)(GROOVY-12823, #3337); -
the parameter of an enclosing call, when the call it is passed to is itself an argument:
take(id(String::trim)), and through further calls,take(wrap(id(String::trim)))(GROOVY-12825, #3339).
Groovy 6.0.0 rejects all of these, so they are additions. This is the only inference this GEP adds, and it is limited to method references, which had no representation before; GEP-34 generalises it to every functional literal.
A method reference operand (list << String::trim) and a method reference branch of a conditional argument
were method closures in Groovy 6.0.0; they stay method closures, with the warning of section 4. Making them
native lambdas removes the method closure’s Closure behaviour, so it is in GEP-34.
4. What is not converted, and the warnings
A targeted literal that cannot be compiled as an implementing closure is stored as in Groovy 6, and the type
checker reports a warning at the LIKELY_ERRORS level: "the closure is stored as a Closure, not as F: a
statically compiled read through F will fail; write 'as F' to convert it". This happens when:
-
the abstract method’s name is a
Closuremethod (call,doCall,isCase,rehydrate,dehydrate, …), including a generic interface whose erased bridge would override one (interface Caller<T> { T call(T t) }), or a default method of the interface clashes with one (getOwner, …); -
the abstract method has its own type parameters;
-
the target is an abstract class, a trait, a sealed interface, or an interface the closure class cannot access;
-
the closure’s parameters do not fit the abstract method.
Runnable, Callable and GroovyCallable need no conversion and get no warning: Closure already implements
them, so a read through them works.
A slot whose type is a type variable of the enclosing method or class (section 1) is not targeted, and gets the same warning, worded for the type variable. Groovy 6 stores a plain closure there without a word; the value is neither the type variable nor its bound, and the failure appears in whichever method reads it.
A value of type Closure, a method pointer or a call result in a type-parameter slot is stored as it is, with
the same warning. Inference of the type variable treats such a value as Groovy 6 does, so
pick(function, closureValue) still infers the common supertype and does not silently choose F.
A warning is chosen over an error because programs that read such values only from dynamic code work today.
5. Raw targets
For a raw target (List<Function>), the interface’s parameters are Object. A typed closure or lambda is
compiled as an implementing closure of the raw interface with an unchecked conversion of its arguments, as a
typed closure for a raw Function parameter is today. A raw receiver (List) binds nothing, so nothing is
converted.
6. Packed closures
An implementing closure has its own class, so GEP-27 cannot pack it. @PackedClosures in its default
LENIENT mode and the groovy.target.closure.pack flag leave it unpacked without a word, and WARN mode and
the pack report give the reason. STRICT mode reports every closure it cannot pack as a compile error, so code
that compiled with 6.0 under STRICT stops compiling. STRICT is opt-in and incubating; this GEP proposes
that it keeps its contract and that the error message names the interface and suggests LENIENT.
Compatibility
All changes are under @CompileStatic, at the sites of sections 1 and 3, compared with Groovy 6.0.0. The
dynamic version of every example behaves as in Groovy 6.0.0.
| Change | Who can notice | Kind |
|---|---|---|
A closure literal or lambda stored at a targeted site is also an instance of the interface |
|
addition |
A statically compiled read through the interface works instead of throwing ClassCastException |
nobody relies on the exception |
fix |
Dynamic code can call the interface’s methods on such a value; a wrapper closure answers the abstract method by calling itself |
code expecting MissingMethodException from |
addition |
Untyped closure and lambda parameters at targeted sites take the interface’s types |
a body that compiled with an |
silent change |
|
builds using |
compile error |
Method references whose type comes from an assignment, return, cast or enclosing call compile |
none |
addition |
New |
builds that treat warnings as errors |
warning |
Code that failed to compile now compiles (untyped literals, conditional and named arguments) |
none |
addition |
@TypeChecked code is compiled as before. No public API is added. The incubating
CompilerConfiguration.LAMBDA_TYPE_ARGUMENT constant added on master by #3089 is removed; it was never
released.
Groovy 6
Groovy 6.0.1 ships method reference arguments only (#3359). The rest of this GEP is not purely additive (see above), so it targets 7.0. Three parts are additive bug fixes and are 6.0.x candidates on their own: #3336, and #3337 with #3339.
Work items
-
Review #3090, #3338 and #3340 as the implementation of sections 1, 2 and 4, rebased on master.
-
Add the wrapper rule of section 2.2 to #3090. The implementing closure’s interface methods otherwise make
andThen,>>,<<,curry,asWritable,memoizeandtrampolinesilently skip the wrapper. -
Extend the implementing-closure path to lambdas that Groovy 6 compiles as closures.
-
Remove the
groovy.target.lambda.typeargoption and the method reference operand path from master, matching 6.0.1. -
Land #3337 and #3339 (section 3), and consider them for 6.0.x.
-
The warnings of section 4, including type-variable slots; no warning for
RunnableandCallable. -
The
@PackedClosures(mode = STRICT)message of section 6. -
Land #3336 independently, and consider it for 6.0.x.
-
A test class over site × literal kind × static and dynamic compilation with the expected representation and read behaviour, and the same cases checked against javac where Java has them.
-
Documentation: "Method references for a type parameter" in
core-semantics.adocbecomes "Functional literals for a type parameter"; a Groovy 7 compatibility entry.
Where this leads
After this GEP, statically compiled code can store closures, lambdas and method references at type-parameter slots and read them back through the interface, and almost nothing that worked in Groovy 6 stops working. What remains different from Java is addressed elsewhere:
-
Two representations of a lambda.
list[0] = λis a native lambda;list.add(λ)is an implementing closure. GEP-34 makes every targeted lambda and method reference native. -
Inference from context. Lambdas and closures still need explicit types where javac infers them, as in
Arrays.asList(s → ..)andList<F> l = [λ]. GEP-34 generalises section 3 to every functional literal. -
Calling native lambdas and proxies from dynamic code.
f(x)fails on a native lambda, which dynamic code already receives fromF f = λand from Java libraries, and on a coercion proxy. GEP-35 adds call syntax for both, and is independent of this GEP and of GEP-34.
Alternatives considered
- Native lambdas now (version 1 of this GEP)
-
Gives Java’s representation at once, but removes call syntax and closure behaviour from values that dynamic code reads today. Moved to GEP-34, with call syntax in GEP-35.
- Keep the 6.0 option
-
Lambdas native only under a JVM system property, so the representation depends on the build. Rejected.
- Convert values as well as literals
-
A new proxy per conversion breaks
contains,removeandequalson the stored value. Rejected. - Coerce at the read site
-
A statically compiled read of an erased slot whose static type is a functional interface would convert a
Closurefound there, leaving the stored value as in Groovy 6. Weaker than this GEP:instanceof Fstays false, Java readers still fail, and a read converts each time unless the conversion is cached per closure. It is the agreed fallback if implementing closures are not accepted, and could sit behind an incubating flag safely: only the reading code changes, so libraries compiled with and without it hand out the same values. - Call
doCalldirectly from the interface method -
About twice as fast per call as the proxy-equivalent body, but a wrong argument through a raw view then fails with ClassCastException in the bridge instead of Groovy 6’s MissingMethodException. Neither form honours a metaclass that intercepts the closure on that path. Rejected.
- Implement the bound at a type-variable slot
-
Moves the failure from readers of the bound to readers of a subinterface, with no gain. Rejected in favour of the warning.
- A wrapper answers nothing
-
Restores Groovy 6’s MissingMethodException for
applyon a wrapper. Consistent, but leavesf.andThen(up).apply(x)failing where statically compiled code composing throughFunctionworks. Rejected in favour of section 2.2. - A compile error instead of the section 4 warning
-
Sound, but breaks programs that read such values only from dynamic code. Could be added later under a strict mode; GEP-34 makes it an error for lambdas, as javac does.
Open questions
-
Should a closure literal added to a
List<? super F>be accepted as an implementing closure ofF? Since GROOVY-12714 it is a compile error, because aClosureis not anF. An implementing closure is anF, so accepting it is sound, as Java accepts an anonymous class there. -
Should
@PackedClosures(mode = STRICT)exempt closures that are unpacked only because they implement an interface, so that section 6 is not a break? -
Should a wrong argument through the
Closureview of an implementing closure be a MissingMethodException at the call, as it is through the interface, rather than a ClassCastException in the body? That needsdoCallto declare the interface’s parameter types, which changes how the closure takes arguments in dynamic code. The ClassCastException matches what a native lambda for a generic interface does (GEP-35, section 1), so the current proposal keeps it.
References and useful links
-
GROOVY-10979, GROOVY-12561, GROOVY-12714, GROOVY-12727, GROOVY-12748, GROOVY-12822, GROOVY-12823, GROOVY-12824, GROOVY-12825
-
Pull requests #3089, #3090, #3336, #3337, #3338, #3339, #3340, #3359
Update history
- 1 (2026-11-25)
-
Initial draft, covering native lambdas, context inference and runtime call syntax.
- 2 (2026-11-25)
-
Reduced to the additive core after an adversarial review; native lambdas, context inference and runtime call syntax moved to GEP-34.
- 3 (2026-11-26)
-
Corrected from running every example on 5.1.3, 6.0.0, 6.0.1 and a build of this GEP: the three changes that are not additions are listed;
list += xis no longer a targeted site; the run-time behaviour of implementing closures in dynamic code is specified, with the wrapper-closure rule; method references take their type from an assignment, return, cast or enclosing call (#3337, #3339); type-variable slots are not targeted and warn;RunnableandCallabledo not warn; packed closures; call syntax for lambdas and proxies moved to GEP-35. The interface method calls the closure as a coercion proxy would, so a wrong argument is a MissingMethodException as in Groovy 6; read-site coercion is the fallback. - 4 (2026-11-27)
-
Section 2.2 states where the interface’s parameter types are checked: by the static type, by a call through the interface, and not through the
Closureview; an open question on theClosureview.