[ 
https://issues.apache.org/jira/browse/GROOVY-12561?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151334#comment-18151334
 ] 

ASF GitHub Bot commented on GROOVY-12561:
-----------------------------------------

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.
   




> SC: a closure passed or assigned for a type parameter bound to a functional 
> interface is stored unconverted
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12561
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12561
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> Under {{@CompileStatic}}, a closure written for a slot declared with a type 
> parameter is stored as the closure itself when the call or assignment binds 
> the type parameter to a functional interface. This happens for an argument, 
> such as {{E}} of {{List.add(E)}} or {{V}} of {{Map.put(K, V)}}, and for an 
> assignment, through {{putAt}} of a {{Map<String, Function<String, String>>}} 
> or a {{List<Supplier<String>>}}, or through the setter or field of a 
> {{Holder<T>}} property for a {{Holder<Supplier<String>>}}. The erasure of the 
> slot is {{Object}}, so nothing converts the closure, and a later call of the 
> interface method fails:
> {code:groovy}
> import java.util.function.Function
> @groovy.transform.CompileStatic
> Map<String, Function<String, String>> make() {
>     Map<String, Function<String, String>> map = [:]
>     map['shout'] = { String s -> s + '!' }
>     map.put('whisper') { String s -> s.toLowerCase() }
>     map
> }
> make().shout.apply('hi') // MissingMethodException: No signature of method: 
> apply ... (a Closure was stored)
> {code}
> The same holds for {{list.add { ... }}}, {{list << { ... }}}, varargs, 
> constructor arguments, {{map?['k'] = ...}}, {{map.k = ...}}, {{holder.value = 
> ...}}, {{box.@field = ...}} and the named constructor argument {{new 
> Holder<Supplier<String>>(value: { ... })}}. Assigning to a variable of the 
> interface type converts the closure.
> Expected: the closure is converted to the interface the call or assignment 
> binds the type parameter to, as it is for a variable of that type.
> Lambdas and method references for such a parameter are GROOVY-10979.
> PR: https://github.com/apache/groovy/pull/3090



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to