Scott Murphy Heiberg created GROOVY-12824:
---------------------------------------------

             Summary: STC: a closure written as an operator operand, a branch 
of a conditional argument or a named constructor argument gets no parameter 
types from the functional interface
                 Key: GROOVY-12824
                 URL: https://issues.apache.org/jira/browse/GROOVY-12824
             Project: Groovy
          Issue Type: Bug
            Reporter: Scott Murphy Heiberg


{code:groovy}
import java.util.function.Function

class Bean { Function<String, String> value }
class Sink { Function<String, String> last; Sink leftShift(Function<String, 
String> f) { last = f; this } }

@groovy.transform.CompileStatic
class C {
    static String apply(Function<String, String> f) { f.apply('a') }
    static List<String> test(boolean flag) {
        def sink = new Sink()
        sink << { it.toUpperCase() }
        [apply(flag ? { it.toUpperCase() } : { it.trim() }),
         new Bean(value: { it.toUpperCase() }).value.apply('a'),
         sink.last.apply('a')]
    }
}
assert C.test(true) == ['A', 'A', 'A']
{code}

With 6.0.0 and master, under @CompileStatic or @TypeChecked, each of the three 
closures is rejected:
{noformat}
[Static type checking] - Cannot find matching method 
java.lang.Object#toUpperCase(). Please check if the declared type is correct 
and if the method exists.
{noformat}

A closure passed as an argument, or assigned to a variable or property of a 
functional interface type, gets the types of its parameters from the interface, 
so its implicit parameter is a String. A closure written in these three places 
does not:
* as the operand of an operator method, such as leftShift(Function<String, 
String>);
* as a branch of a conditional expression passed as an argument;
* as a named argument of a constructor call, for a property of the interface 
type.

The same holds for a lambda with an untyped parameter.

In each, the type checker visits the closure before it knows the operator 
method, the method or the property: the right operand of an operator before its 
method is looked up, an argument that is not itself a closure before the method 
is chosen, and the map of named arguments before the properties are checked.




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

Reply via email to