[ 
https://issues.apache.org/jira/browse/GROOVY-12561?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Scott Murphy Heiberg updated GROOVY-12561:
------------------------------------------
    Description: 
Under {{@CompileStatic}}, a closure assigned to a subscript or a property whose 
type is a functional interface is stored as the closure itself when the method 
that stores it declares its parameter with a type parameter: {{putAt}} of a 
{{Map<String, Function<String, String>>}} or {{List<Supplier<String>>}}, or the 
setter or field of a {{Holder<T>}} property for a {{Holder<Supplier<String>>}}. 
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
}
make().shout.apply('hi') // MissingMethodException: No signature of method: 
apply ... (a Closure was stored)
{code}

The same holds for {{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 type of the subscript or property, as 
for a variable, and the value of the assignment is the converted closure.

PR: https://github.com/apache/groovy/pull/3090

  was:
Under {{@CompileStatic}}, a closure assigned to a subscript or a property whose 
type is a functional interface is stored as the closure itself when the method 
that stores it declares its parameter with a type parameter: {{putAt}} of a 
{{Map<String, Function<String, String>>}} or {{List<Supplier<String>>}}, or the 
setter or field of a {{Holder<T>}} property for a {{Holder<Supplier<String>>}}. 
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
}
make().shout.apply('hi') // MissingMethodException: No signature of method: 
apply ... (a Closure was stored)
{code}

The same holds for {{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 type of the subscript or property, as 
for a variable, and the value of the assignment is the converted closure.


> SC: a closure assigned to a subscript or property of a functional interface 
> type 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 assigned to a subscript or a property 
> whose type is a functional interface is stored as the closure itself when the 
> method that stores it declares its parameter with a type parameter: {{putAt}} 
> of a {{Map<String, Function<String, String>>}} or {{List<Supplier<String>>}}, 
> or the setter or field of a {{Holder<T>}} property for a 
> {{Holder<Supplier<String>>}}. 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
> }
> make().shout.apply('hi') // MissingMethodException: No signature of method: 
> apply ... (a Closure was stored)
> {code}
> The same holds for {{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 type of the subscript or property, 
> as for a variable, and the value of the assignment is the converted closure.
> PR: https://github.com/apache/groovy/pull/3090



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

Reply via email to