GEP-34


Metadata
Number

GEP-34

Title

Native Functional Values

Version

2

Type

Feature

Status

Draft

Target

Groovy 7.0

Comment

Builds on GEP-33 and needs GEP-35. Under @CompileStatic, a lambda or method reference becomes the same native object wherever it is written, and a call’s target can come from its context, as in Java. Contains the breaking changes GEP-33 avoids, with the benefits they buy and a way to find affected code.

Leader

Paul King

Created

2026-11-25

Last modification

2026-11-26

Note
WARNING: Material on this page is still under development! This proposal is a draft for discussion. An implementation of sections 1 to 3 passes the core test suite; the behaviour tables come from running each example on Groovy 6.0.0 and on builds of GEP-33 and of this GEP, and from javac 17 for the Java comparisons.

Abstract

GEP-33 makes functional literals stored at type-parameter slots usable through their interface without taking anything away. It leaves Groovy with rules a Java developer would not predict: the same lambda is a native lambda at list[0] = λ and a closure at list.add(λ), and Arrays.asList(s → ..) or List<F> l = [λ] need explicit types.

This GEP proposes, for statically compiled code:

  1. every lambda and method reference with a functional-interface target is a native lambda, as in Java;

  2. the target of a generic call can come from its context (assignment, return, argument of an enclosing call, collection literal), as in JLS 18.5.2;

  3. @TypeChecked code accepts the same literals as statically compiled code and never stores one that a read through the interface would reject.

The first item removes closure behaviour from lambdas and method references that dynamic code may read today. It depends on GEP-35, which lets dynamic code call a native lambda with f(x); without it, dynamic code would lose call syntax on every such value. Closure literals are not affected: a closure literal stays a closure everywhere.

The benefit, in full

One representation, the one Java uses

@CompileStatic site Groovy 6.0 GEP-33 GEP-34 (= javac)

F f = λ, take(λ) for an F parameter

native

native

native

list[0] = λ, box.value = λ

native

native

native

list.add(λ), map.put(k, λ), list.set(0, λ)

closure, read through F fails

implementing closure

native

list << λ

closure, read through F fails

implementing closure

native

list << String::trim

method closure, read through F fails

method closure, warning

native

cond ? λ1 : λ2 as an argument

closure, read through F fails

implementing closure

native

Optional.of(String::trim), Arrays.asList(λ), List.of(λ)

compile error

compile error

native

F f = id(id(λ)), pick(fv, String::strip)

compile error

method references only (GEP-33 section 3)

native

List<F> l = [λ], Map<String, F> m = [k: λ]

compile error

compile error

native

list += λ on a List<F>

run-time failure (6.0.0), compile error (6.0.1)

compile error

native

With this GEP, a statically compiled lambda is a native lambda. A reader of the code no longer needs to know which method a lambda is passed to, nor whether that method’s parameter is declared with a type variable.

Java code moves to Groovy unchanged

The rows marked "compile error" above are ordinary Java. Under this GEP they compile, with the same meaning as in Java. The cases Java rejects are rejected too (section 1), with the same reason.

Cheaper functional values

A native lambda is created by LambdaMetafactory: no generated closure class per literal, no Closure object with its owner, delegate and resolve strategy, and no reflective doCall lookup on call. A non-capturing lambda can be hoisted by GEP-27 to a shared instance with no allocation at all. An implementing closure, which GEP-33 uses, keeps all of those costs. Native lambdas also need no proxy or reflection metadata in a GraalVM native image. Creation cost and native-image behaviour are still to be measured (work items).

Type-checked and statically compiled code agree

Under this GEP @TypeChecked and @CompileStatic accept the same programs at these sites, and a program @TypeChecked accepts does not fail at run time because a literal was not converted.

What it costs

Breaking changes

Compared with GEP-33, under @CompileStatic. The dynamic version of every example is unchanged.

Change What stops working What still works

A lambda at a type-parameter slot, operand or conditional argument is native instead of an implementing closure

from dynamic code: instanceof Closure, delegate, curry, memoize, >>, Closure-only DGM methods, serialization unless the target is Serializable. grep and switch use equals, so grep returns [] and switch takes the default branch, with no error

call syntax (GEP-35), the interface method, identity

list << String::trim stores a native lambda, not a method closure

the same, plus .method and the other MethodClosure properties

call syntax (GEP-35), the interface method

A lambda in a closure that calls a method of the closure’s delegate, such as list.with { add) → size( }

compile error: a native lambda cannot reach the delegate

write a closure, or name the receiver

A lambda whose parameters do not fit the interface, a lambda for a method with its own type parameters, a lambda for an abstract class

compile error, as in javac. Groovy 6 stores a closure that no read through the interface could use

a method reference, or a closure with as

A lambda for a slot whose type is a type variable of the enclosing method or class

compile error, as in javac ("T is not a functional interface"). Groovy 6 stores a closure that is not a T

a closure literal, which GEP-33 warns about

Inference from context (section 2)

code whose overload or inferred type changes; listed by the report mode

—

Only lambdas and method references lose closure behaviour. A closure literal stays a closure everywhere, which gives users a simple rule: where dynamic code needs closure behaviour, write a closure literal.

Under @TypeChecked, see section 3.

Finding affected code

The compiler has a report mode, -Dgroovy.target.functional.report=true (CompilerConfiguration.FUNCTIONAL_REPORT), that changes nothing about the generated code. It lists every lambda or method reference whose representation differs from Groovy 6, with its location and the site kind, and every call whose overload choice or inferred type differs because of context inference. It is meant to be run once over a code base before upgrading, and by IDEs.

The usual fix for a reported site is to write the literal as a closure ({ s → s.trim() } instead of s → s.trim()) where dynamic code needs closure behaviour, or to do nothing where it does not.

Proposal

1. Native lambdas and method references

Under @CompileStatic, every lambda or method reference with a functional-interface target is compiled as a native lambda, at every site of GEP-33 section 1, in collection literals with a declared element type, and wherever section 2 finds a target.

  • A target with a secondary bound (<T extends Function<String, String> & Serializable>) gives a lambda that implements every bound, as javac does with altMetafactory. A secondary bound that is itself a functional interface makes the target invalid, as in Java.

  • A target whose abstract method has its own type parameters accepts a method reference but not a lambda (JLS 15.27.3).

  • A target that is a type variable of the enclosing method or class, a capture of a wildcard, or a class is not a functional interface: a lambda for it is a compile error, as in Java.

  • A raw target (List<Function>) has Object parameters: a typed lambda is rejected, as in Java.

  • A targeted lambda that cannot be compiled for its target is a compile error, not a silent closure. A method reference whose target cannot be found is rejected as in Groovy 6.

The generic interface whose erased bridge collides with Closure.call, and the interface whose abstract method shares a Closure method’s parameters, which GEP-33 cannot implement with a closure (its section 4), are no problem for a native lambda, which does not extend Closure.

2. Inference from context

The type arguments of a generic call are inferred from its arguments and its context together:

  • the context is an assignment, a return, a cast, a conditional branch, an element of a collection literal, or a parameter of an enclosing call, through any number of nested calls (take(id(id(λ))));

  • the overload of the enclosing call is chosen as in Groovy 6. A functional literal whose target depends on that choice is then typed for the chosen parameter. Choosing the overload with the literal "not pertinent to applicability" (JLS 15.12.2.2) was tried and rejected: it changes which overload Groovy 6 chooses for programs such as println(K.id { 1 });

  • a value of type Closure is a witness like any other value, so pick(function, closureValue) infers the common supertype as in Groovy 6 and is not silently treated as F;

  • where the rule finds no target, the literal is compiled as GEP-33 compiles it.

GROOVY-12727 (assignment, return and cast targets for lambdas and closures) and GEP-33 section 3 (the same, and enclosing calls, for method references; #3337 and #3339) are special cases of this rule. The implementation must replace their mechanism with the one protocol of this section, not keep two.

The implementation checks a call whose arguments are pending, and resumes it when the enclosing call is chosen. Type-checking extensions must see each call once: the provisional check must not reach beforeMethodCall or onMethodSelection. This is a requirement still to be tested.

Known gaps in the implementation, to close or to list as limits: an element of a list literal passed as an argument (take([λ])), a diamond constructor (new Box<>(λ)), and nested shapes of these.

3. Type-checked code

@TypeChecked code is compiled dynamically, so it cannot store native lambdas. At the sites of section 1:

  • an untyped closure literal or lambda takes its parameter types from the interface, as under @CompileStatic, so list.add { it.toUpperCase() } compiles on a List<Function<String, String>>, as dynamic Groovy already runs it;

  • a closure literal or lambda is stored as an implementing closure (GEP-33 section 2), which dynamic code handles like any closure;

  • a method reference at a type-parameter slot stays a method closure, with GEP-33’s warning, at every site where Groovy 6 compiled it. Version 1 of this GEP made list << String::trim and list[0] = String::trim a compile error under @TypeChecked; they work in Groovy 6, so that is withdrawn.

Implementation

An implementation of sections 1 to 3 (November 2026), on top of GEP-33 and GEP-35, passes the full core test suite. Closure literals at declared targets are proxies again (Deferred: closure literals at declared targets), and section 2’s protocol is the only inference mechanism: it replaces the code paths of #3337 and #3339, whose tests pass unchanged, and type-checking extensions see each call once. Still open:

  • the gaps of section 2;

  • a subscript or operand on a List<?> (l[0] = (s → s), l << (s → s)) still stores a closure, as in Groovy 6, while an argument is rejected;

  • a closure literal assigned by subscript to a type-variable slot (l[0] = { .. } on a List<T>) gets no warning.

Two bugs found on the way are older than this GEP: a lambda whose parameter is declared with a superinterface of the interface’s parameter type, (CharSequence s) → .. for a Function<String, String>, fails in the lambda writer in Groovy 6.0.0 too (fixed in the implementation); and an ExpandoMetaClass that adds a method to an interface after a class implementing it has its own ExpandoMetaClass fails with NullPointerException in MetaMethodIndex on Groovy 5.1.3 and later, with no closure involved (to be reported separately).

Deferred: closure literals at declared targets

Version 1 proposed that a closure literal with a declared functional-interface target (Function<String, String> f = { .. }, take { .. } for an F parameter) be an implementing closure instead of a coercion proxy, for its identity and closure behaviour. Running the examples showed that it changes, with no error, what dynamic code does with such values:

  • overload choice: which(f) with overloads which(Closure) and which(Function) selects which(Closure);

  • grep and switch call the value instead of comparing it;

  • the closure can no longer be packed (GEP-27), and a metaclass that intercepts doCall is bypassed when the value is called through the interface.

The benefit is mostly identity, and the most visible gap of the proxy, that dynamic f() fails, is closed by GEP-35 without any of these changes. The change also overlaps the compile-time adapter of GROOVY-12492. It is deferred until those two are reconciled.

Work items

  1. Native lambdas and method references at every targeted site, with secondary bounds and the compile errors of section 1, including type-variable targets.

  2. Inference from context (section 2), replacing the mechanisms of GROOVY-12727, #3337 and #3339, with no extension callbacks for the provisional check, and the gaps of section 2 closed or documented.

  3. Type-checked code (section 3), with method references kept as method closures.

  4. Remove implementing closures at declared targets from the implementation (Deferred: closure literals at declared targets).

  5. The report mode for finding affected code.

  6. The lambda writer fix for parameters declared with a superinterface, also a 6.0.x candidate.

  7. Benchmarks: closure, implementing closure, proxy and native lambda, for creation and call, on the JVM and in a native image.

  8. Cases checked against javac in GenericsJavaCompatibilityTest, so "as in Java" is tested.

  9. Documentation and a Groovy 7 compatibility entry.

Alternatives considered

Stop at GEP-33

Nothing breaks, but the two representations of a lambda and the gaps in inference remain, and lambdas keep the cost of closures at type-parameter slots.

Native lambdas without GEP-35

Smaller, but dynamic code loses f() on every lambda it receives from statically compiled code. Rejected: this GEP needs GEP-35.

An option for the old behaviour

The representation would depend on the build. The report mode finds affected code instead.

Compile lambdas as implementing closures everywhere

Keeps closure behaviour for lambdas, but gives up the native form and GEP-27 hoisting even where Groovy 6 already uses it. Rejected.

Choose overloads with literals not pertinent to applicability

JLS 15.12.2.2. Changes Groovy 6 overload choice; rejected (section 2).

Open questions

  • Should GEP-33 and this GEP land in the same release, so that no release ships lambdas as implementing closures at sites that later become native? If they cannot, GEP-33 could leave lambdas as Groovy 6 does and convert only closure literals.

  • Should inference from context (section 2) be its own GEP? It does not depend on native lambdas: with GEP-33’s representation it would type closures and lambdas as well.

  • Should a closure literal at a type-variable slot also be a compile error, under a strict mode?

Update history

1 (2026-11-25)

Initial draft, from the parts of GEP-33 version 1 that change existing behaviour.

2 (2026-11-26)

From running every example on 6.0.0 and builds of GEP-33 and this GEP: the full list of breaking changes; type-variable targets; overload choice kept as in Groovy 6; @TypeChecked method references kept as method closures; #3337 and #3339 as special cases of section 2; closure literals at declared targets deferred; call syntax moved to GEP-35, on which this GEP depends.