groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-8241) SAM parameter type inference for explicit parameter
Date Mon, 18 Dec 2017 10:20:00 GMT

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

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

Github user avafanasiev commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/643#discussion_r157445258
  
    --- 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 --
    
    Actually you can't pass anything  except String to Predicate<? super String> p .
Cause it should be able to be assigned to captured type '? super String' . This behavior matches
java's lambda behavior.


> 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
>             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
(v6.4.14#64029)

Mime
View raw message