Repository navigation
GROOVY-12835: call syntax on functional values (GEP-35) - #3363
Open
paulk-asert wants to merge 2 commits into
Open
paulk-asert wants to merge 2 commits into
paulk-asert wants to merge 2 commits into
Conversation
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.
✅ All tests passed ✅🏷️ Commit: 411dfb6 Learn more about TestLens at testlens.app/docs. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: fromFunction<String, String> f = s -> s.trim()under@CompileStatic, fromString::trim, from Java libraries (Function.identity()), and from{ it } as Function. This PR lets dynamic code call such a value withf(args)orf.call(args), invoking the abstract method of its functional interface.def f = Function.identity(); f(' a ')' a 'f.call(' a ')on the same value' a 'def p = { it } as Function; p(' a ')' a '[1, 2](),new Thread()(),Comparator.naturalOrder()(1, 2)Which values
LambdaMetafactorycreates;java.lang.reflect.Proxywhose 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(throughIterable), aThreadorRunnable, anInputStream(throughAutoCloseable), 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, acallproperty, 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 toobj.f(x)for a propertyfholding 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 hotf(x)does not take the missing-method path.NativeImageMetadataGenerator: ships the reflection metadata a native image needs for the coercion-proxy guardSelectorlooks 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)f(x)on a native lambda, bound at the call sitef.apply(x)on the same lambdac(x)on a closuref(x)on a coercion proxyTesting
FunctionalValueCallSyntaxTest: every value kind, the excluded objects,methodMissingandinvokeMethodprecedence, properties, wrong arity, and the indy and non-indy paths.:testpasses: 22554 tests, 0 failures, 332 skipped.checkstyleGateand:tests-native:checkNativeMetadatapass.GEP-35 is independent of GEP-33 (#3362), and is needed by GEP-34, which makes statically compiled lambdas native at more sites.