paulk-asert opened a new pull request, #3362: URL: https://github.com/apache/groovy/pull/3362
JIRA: [GROOVY-12561](https://issues.apache.org/jira/browse/GROOVY-12561), [GROOVY-10979](https://issues.apache.org/jira/browse/GROOVY-10979), [GROOVY-12823](https://issues.apache.org/jira/browse/GROOVY-12823), [GROOVY-12825](https://issues.apache.org/jira/browse/GROOVY-12825) The implementation of [GEP-33: Functional Literals for Type-Parameter Slots](https://groovy.apache.org/wiki/GEP-33.html) (version 3). Under `@CompileStatic`, a closure literal or lambda written for a slot declared with a type parameter that the call binds to a functional interface, such as `E` of `List.add(E)` on a `List<Supplier<String>>`, is compiled as a closure class that also implements the interface. The value stays a `Closure`, so dynamic code reads it as before, and a statically compiled read through the interface (`list[0].get()`) works instead of throwing ClassCastException. ## Supersedes This branch contains, unchanged and with their authorship, the commits of Scott Murphy Heiberg's PRs: - #3090 (GROOVY-12561): closure literals for type-parameter slots implement the interface - #3338 (GROOVY-12824): closure parameter types from the interface for operands, conditional and named arguments - #3340 (GROOVY-12561): the same for operands and conditional arguments - #3336 (GROOVY-12822): a conditional of lambdas or method references assigned to a functional interface - #3337 (GROOVY-12823) and #3339 (GROOVY-12825): method references take their type parameter from an assignment, return or cast target, or from the parameter of an enclosing call, as in `take(id(String::trim))` plus a merge of master that includes #3089, which the last review on #3090 asked for. If this is merged, those PRs can be closed. ## What changed relative to the reviewed #3090 1. **Lambdas** for type-parameter slots are compiled as implementing closures too, and the incubating `groovy.target.lambda.typearg` option and the method reference operand path from #3089 are removed. A method reference argument is still compiled for the interface; a method reference operand stays a method closure, with a warning. 2. **Wrapper closures.** A composed, curried, writable, memoized or trampoline closure keeps the closure it wraps as its owner or in a field. With the interface method lookup that #3090 adds to `ClosureMetaClass`, a dynamic `f.andThen(up).apply(x)` fell through to the wrapped closure and ran only `f`, and `b.curry('x', 'y').apply(p, q)` dropped the curried arguments, with no error. A wrapper now answers the interface's abstract method by calling itself; default methods are not answered. 3. **The interface method's body** calls the closure as a coercion proxy would (the `SamAdapterFactory` adapter body from GROOVY-12492), so a wrong argument through a raw view is a MissingMethodException as in Groovy 6, not a ClassCastException from the bridge. 4. **`@PackedClosures`**: the decline reason names the interface, and under `mode = STRICT` the error suggests `LENIENT` or `WARN`, since such code compiled with `STRICT` before. 5. **A new LIKELY_ERRORS warning** for a closure stored in a slot whose type is a type variable of the enclosing method or class with a functional bound (`<T extends Function<..>> void fill(List<T> l) { l.add { .. } }`): the value is neither a `T` nor the bound. ## Compatibility All changes are under `@CompileStatic`. Two are not additions: an untyped closure parameter now takes the interface's type, so a call in its body can select a different overload (the one dynamic Groovy already selects), and `@PackedClosures(mode = STRICT)` rejects such a closure. The GEP's compatibility section lists every change, with examples run on 5.1.3, 6.0.0, 6.0.1 and this branch. ## Testing - The branch tip is the tree on which the full root `:test` passed: 22565 tests, 0 failures, 332 skipped. `checkstyleGate` passes. - Each commit was checked with the functional-literal, method reference, lambda, closure and coercion test classes. - New tests in `FunctionalLiteralSiteTest`, `FunctionalTypeArgumentTest` and `MethodReferenceTest`. - The branch merges cleanly with current master. -- 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]
