You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/02/14 18:48:00 UTC

[jira] [Commented] (GROOVY-8241) SAM parameter type inference for explicit parameter

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

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

Github user daniel-huss commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/643#discussion_r168272512
  
    --- Diff: src/test/groovy/transform/stc/MethodCallsSTCTest.groovy ---
    @@ -383,6 +406,94 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
                 ''', 'Reference to method is ambiguous'
         }
     
    +    void testShouldFailWithMultiplePossibleMethods2() {
    +        shouldFailWithMessages '''
    +                static String foo(String s) {
    +                    'String'
    +                }
    +                static String foo(Integer s) {
    +                    'Integer'
    +                }
    +                static String foo(Boolean s) {
    +                    'Boolean'
    +                }
    +                ['foo',123,true].each { argument ->
    +                    if (argument instanceof String || argument instanceof Boolean || argument instanceof Integer) {
    +                        foo(argument)
    +                    }
    +                }
    +            ''', 'Reference to method is ambiguous'
    +    }
    +
    +    void testInstanceOfOnExplicitParameter() {
    +        assertScript '''
    +                1.with { obj ->
    +                    if (obj instanceof String) {
    +                        obj.toUpperCase() 
    +                    }
    +                }
    +            '''
    +    }
    +
    +    void testSAMWithExplicitParameter() {
    +        assertScript '''
    +            public interface SAM {
    +                boolean run(String var1, Thread th);
    +            }
    +            
    +            static boolean foo(SAM sam) {
    +               sam.run("foo",  new Thread())
    +            }
    +            
    +            static def callSAM() {
    +                foo { str, th ->
    +                    str.toUpperCase().equals(th.getName())
    +                }
    +            }
    +            '''
    +    }
    +
    +    void testGroovy8241() {
    +        assertScript '''
    +            import java.util.function.Predicate
    +            
    +            static boolean foo(Predicate<? super String> p) {
    +                p.test("foo")
    +            }
    +            
    +            static def testPredicate() {
    +                foo { it ->
    +                    it.toUpperCase()
    --- End diff --
    
    Oh, you're right. I forgot Groovy's interpretation of <? super X> is simply <X> :)


> SAM parameter type inference for explicit parameter
> ---------------------------------------------------
>
>                 Key: GROOVY-8241
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8241
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 2.4.10
>            Reporter: Daniil Ovchinnikov
>            Assignee: Daniel Sun
>            Priority: Major
>             Fix For: 2.5.0-beta-3, 2.6.0-alpha-3, 2.4.14, 3.0.0-alpha-2
>
>
> {code}
> import groovy.transform.CompileStatic
> import java.util.function.Predicate
> @CompileStatic
> static boolean foo(Predicate<? super String> p) {
>     p.test("foo")
> }
> @CompileStatic
> static def testPredicate() {
>     foo { // it ->
>         it.toUpperCase()
>         true
>     }
> }
> {code}
> Uncomment {{it}}, compiler will say: 
> {noformat}
> Cannot find matching method java.lang.Object#toUpperCase()
> {noformat}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)