Skip to content

GROOVY-12835: call syntax on functional values (GEP-35) - #3363

Open
paulk-asert wants to merge 2 commits into
apache:masterfrom
paulk-asert:gep35
Open

paulk-asert wants to merge 2 commits into
apache:masterfrom
paulk-asert:gep35

Conversation

@paulk-asert

@paulk-asert paulk-asert commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

JIRA: GROOVY-12835

The implementation of GEP-35: Call Syntax on Functional Values.

Dynamic Groovy calls a closure with f(x), but not a native lambda or a coercion proxy, although dynamic code receives both today: from Function<String, String> f = s -> s.trim() under @CompileStatic, from String::trim, from Java libraries (Function.identity()), and from { it } as Function. This PR lets dynamic code call such a value with f(args) or f.call(args), invoking the abstract method of its functional interface.

Dynamic code master this PR
def f = Function.identity(); f(' a ') MissingMethodException ' a '
f.call(' a ') on the same value MissingMethodException ' a '
def p = { it } as Function; p(' a ') MissingMethodException ' a '
[1, 2](), new Thread()(), Comparator.naturalOrder()(1, 2) MissingMethodException MissingMethodException
a lambda with the wrong number of arguments MissingMethodException MissingMethodException

Which values

  • a native lambda or method reference: an instance of a class LambdaMetafactory creates;
  • a Groovy coercion proxy: a java.lang.reflect.Proxy whose invocation handler is Groovy's conversion handler, checked per instance, since a proxy class is shared by every proxy for the same interfaces.

No other object is callable: in particular not a List (through Iterable), a Thread or Runnable, an InputStream (through AutoCloseable), or a user class implementing a functional interface. A wider rule, "any object implementing exactly one functional interface", made [1, 2]() return an iterator and was rejected.

When

The rule runs after everything the MOP already tries (categories, methodMissing, invokeMethod, a call property, the outer class of an inner class), as a last resort before MissingMethodException, so the message of that exception is unchanged for every other object. It applies to call syntax on the value itself, not to obj.f(x) for a property f holding a lambda, nor to a map entry (commands.shout('hi')); GEP-35 leaves that as an open question. Statically compiled code is not affected: GROOVY-8466 already rewrites call syntax on a receiver typed with a functional interface.

Implementation

  • org.apache.groovy.internal.metaclass.FunctionalCallSupport (internal): finds the method a functional value's call syntax stands for.
  • MetaClassImpl: consults it only when a call is about to fail with MissingMethodException.
  • Selector: binds such a call at the call site, so a hot f(x) does not take the missing-method path.
  • NativeImageMetadataGenerator: ships the reflection metadata a native image needs for the coercion-proxy guard Selector looks up.

Compatibility

Code that tells callable values from others by catching MissingMethodException, as in try { value(x) } catch (MissingMethodException e) { .. }, now finds lambdas and coercion proxies callable. One test on master pinned the old behaviour for a lambda stored by statically compiled code, and is updated.

Performance (JMH, subprojects/performance)

Call ns per call
dynamic f(x) on a native lambda, bound at the call site 1.04
dynamic f.apply(x) on the same lambda 0.90
dynamic c(x) on a closure 0.98
dynamic f(x) on a coercion proxy 10.8
the same call through the meta-class, without the call-site binding 195

Testing

  • FunctionalValueCallSyntaxTest: every value kind, the excluded objects, methodMissing and invokeMethod precedence, properties, wrong arity, and the indy and non-indy paths.
  • The full root :test passes: 22554 tests, 0 failures, 332 skipped. checkstyleGate and :tests-native:checkNativeMetadata pass.
  • The branch is based on master and merges cleanly with the current tip.

GEP-35 is independent of GEP-33 (#3362), and is needed by GEP-34, which makes statically compiled lambdas native at more sites.

Dynamic code can now call a native lambda or method reference (an
instance of a class LambdaMetafactory creates) and a Groovy coercion
proxy (a Proxy whose handler is Groovy's conversion handler, checked per
instance) with f(args) or f.call(args): the call invokes the abstract
method of the value's functional interface. No other object becomes
callable; a List, a Thread, an InputStream or a user class implementing a
functional interface still throws MissingMethodException.

The rule runs only after everything the MOP already tries, as a last
resort before MissingMethodException, so categories, methodMissing,
invokeMethod and a call property keep their precedence. A call through a
property, obj.f(x), is not affected. The invokedynamic Selector binds the
call at the call site, so a hot f(x) costs about as much as f.apply(x).

This implements GEP-35.
Compares dynamic f(x) and f.apply(x) on a native lambda, a closure call,
a coercion proxy, and the meta-class path without the call-site binding.
@paulk-asert paulk-asert changed the title GEP-35: call syntax on functional values GROOVY-12835: call syntax on functional values Nov 26, 2026
@paulk-asert paulk-asert changed the title GROOVY-12835: call syntax on functional values GROOVY-12835: call syntax on functional values (GeP-35) Nov 26, 2026
@testlens-app

testlens-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 411dfb6
▶️ Tests: 145509 executed
⚪️ Checks: 19/19 completed


Learn more about TestLens at testlens.app/docs.

@paulk-asert paulk-asert changed the title GROOVY-12835: call syntax on functional values (GeP-35) GROOVY-12835: call syntax on functional values (GEP-35) Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant