paulk-asert opened a new pull request, #3363: URL: https://github.com/apache/groovy/pull/3363
The implementation of [GEP-35: Call Syntax on Functional Values](https://groovy.apache.org/wiki/GEP-35.html). There is no JIRA issue yet; the commits will reference one once it exists. 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. ## 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` passes. - 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
