paulk-asert opened a new pull request, #3364: URL: https://github.com/apache/groovy/pull/3364
The implementation of [GEP-34: Native Functional Values](https://groovy.apache.org/wiki/GEP-34.html) (version 2). There is no JIRA issue yet; the commit will reference one once it exists. **This PR is stacked.** It contains #3362 (GEP-33) and #3363 (GEP-35, GROOVY-12835), which it needs, and adds one commit: the last, `GEP-34: ...`. Review that commit; the others are reviewed on their own PRs. The only conflict in stacking them is one hunk of `FunctionalTypeArgumentTest`, where #3362's side is kept. Once #3362 and #3363 are merged, this branch will be rebased to the one commit. ## What changes Under `@CompileStatic`, a lambda or method reference whose target is a functional interface is a native lambda, as in Java, at sites where Groovy 6 stored a closure or rejected it: | `@CompileStatic` site | Groovy 6.0 | GEP-33 (#3362) | this PR | |---|---|---|---| | `list.add(λ)`, `map.put(k, λ)`, `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 | native | | `List<F> l = [λ]`, `Map<String, F> m = [k: λ]` | compile error | compile error | native | - **Inference from context.** The target of a generic call can come from the type it is assigned, returned or cast to, or from the parameter of an enclosing call. One mechanism serves lambdas and method references: the call waits for its target type and is resumed, never checked twice, so type-checking extensions see it once (tested with the extension callbacks). It replaces the deferred method references of GROOVY-12823/12825 (#3337, #3339) that #3362 contains. - **`@TypeChecked`** accepts the same literals. A closure or lambda there is a closure that implements the interface; a method reference at an operand, a subscript or a conditional branch stays a method closure with GEP-33's warning, and `list.add(String::trim)` stays an error, as in Groovy 6. - **Type-variable targets.** A lambda for a slot whose type is a type variable is a compile error, as in javac: "The lambda is written for T, which is a type variable: T is not a functional interface; write a closure, or take the value as a parameter of type T". A closure literal there keeps GEP-33's warning, now also at operands. - **Closure literals at declared targets** (`F f = { .. }`, `take { .. }`) stay coercion proxies, as GEP-34 v2 defers implementing closures there. - **Report mode.** The incubating system property `groovy.target.functional.report=true` makes the type checker warn of each literal that is now a native lambda where Groovy 6 had a closure, a method closure or a proxy, and of each call whose type arguments come from its context. It does not change the generated code (tested). - **A lambda writer fix**, also a 6.0.x candidate: a lambda parameter declared with a superinterface of the interface method's parameter type (`Function<String, ..>` with `(CharSequence s) -> ..`) failed with "The inferred type[String] is not compatible with the parameter type[CharSequence]", also in 6.0.0. ## Compatibility These are breaking changes for statically compiled code that hands such values to dynamic code; GEP-34's "Breaking changes" section lists them with what still works. In short: a native lambda is not a `Closure`, so `instanceof Closure`, `curry`, `>>`, `delegate`, `Closure`-only DGM methods and serialization (unless the target is `Serializable`) stop working on it from dynamic code, and `grep` and `switch` silently stop matching it. Call syntax keeps working through GEP-35 (#3363). A lambda that calls a method of an enclosing closure's delegate, a lambda whose parameters do not fit the interface, and a lambda for a type variable become compile errors, as in javac. The report mode finds affected code. ## Not in this PR - `list += λ` on a `List<F>` is still a compile error; the GEP's table lists it as native. - The GEP's work items for benchmarks, cases checked against javac in `GenericsJavaCompatibilityTest`, and a Groovy 7 compatibility entry. ## Testing - New `FunctionalContextInferenceTest`; updated `FunctionalLiteralSiteTest`, `FunctionalTypeArgumentTest`, `CoercionTest` and `RegressionTest`. - On the branch tip, the full root `:test` passes (22628 tests, 0 failures, 332 skipped), as do `checkstyleGate`, `codenarcGate` and `:tests-native:checkNativeMetadata`. - The branch is based on master at the same commit as #3362 and #3363. -- 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]
