You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Yuriy Kiselev (JIRA)" <ji...@apache.org> on 2017/01/04 23:35:58 UTC

[jira] [Updated] (GROOVY-8034) Compile error when using generic type with lower bound

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

Yuriy Kiselev updated GROOVY-8034:
----------------------------------
    Description: 
I'm trying to implement chained generics. It works as Java code but fails to compile by groovyc:
{code}
Error:(38, 9) Groovyc: [Static type checking] - Cannot call <C extends com.github.ykiselev.ChainTest.A<? super O, ?>> com.github.ykiselev.ChainTest$A <? super O -> java.lang.Object, ?>#andThen(C) with arguments [com.github.ykiselev.ChainTest$A <Double, String>]
{code}
Code (JUnit4 unit test)
{code}
package com.github.ykiselev

import groovy.transform.TypeChecked
import org.junit.Test

@TypeChecked
class ChainTest {

    static class A<I, O> {

        def <C extends A<? super O, ?>> C andThen(C next) {
            return next
        }

    }

    static <I, O, C extends A<? super O, ?>> C chain(A<I, O> self, C next) {
        self.andThen(next)
        return next
    }

    @Test
    "should chain"() {
        def a1 = new A<String, Integer>()
        def a2 = new A<Integer, Double>()
        def a3 = new A<Double, String>()
        def a4 = new A<String, Double>()
        def a5 = new A<Number, Object>()

        a1.andThen(a2) // ok
        a2.andThen(a3) // ok
        a3.andThen(a4) // ok
        a4.andThen(a5) // ok (even without "? super O")

        chain(a1, a2).andThen(a3) // ok
        chain(chain(chain(chain(a1, a2), a3), a4), a5) // ok

        a1.andThen(a2)
                .andThen(a3) // static type checker error
                .andThen(a4)
                .andThen(a5)
    }
}
{code}

This would compile if I change bounded type parameter like this:
{code}
def <C extends A<O, ?>> C andThen(C next) {
            return next
}
{code}

but I need it bounded.

  was:
I'm trying to implement chained generics. It works as Java code but failed to compile by groovyc:
{code}
Error:(38, 9) Groovyc: [Static type checking] - Cannot call <C extends com.github.ykiselev.ChainTest.A<? super O, ?>> com.github.ykiselev.ChainTest$A <? super O -> java.lang.Object, ?>#andThen(C) with arguments [com.github.ykiselev.ChainTest$A <Double, String>]
{code}
Code (JUnit4 unit test)
{code}
package com.github.ykiselev

import groovy.transform.TypeChecked
import org.junit.Test

@TypeChecked
class ChainTest {

    static class A<I, O> {

        def <C extends A<? super O, ?>> C andThen(C next) {
            return next
        }

    }

    static <I, O, C extends A<? super O, ?>> C chain(A<I, O> self, C next) {
        self.andThen(next)
        return next
    }

    @Test
    "should chain"() {
        def a1 = new A<String, Integer>()
        def a2 = new A<Integer, Double>()
        def a3 = new A<Double, String>()
        def a4 = new A<String, Double>()
        def a5 = new A<Number, Object>()

        a1.andThen(a2) // ok
        a2.andThen(a3) // ok
        a3.andThen(a4) // ok
        a4.andThen(a5) // ok (even without "? super O")

        chain(a1, a2).andThen(a3) // ok
        chain(chain(chain(chain(a1, a2), a3), a4), a5) // ok

        a1.andThen(a2)
                .andThen(a3) // static type checker error
                .andThen(a4)
                .andThen(a5)
    }
}
{code}

This would compile if I change bounded type parameter like this:
{code}
def <C extends A<O, ?>> C andThen(C next) {
            return next
}
{code}

but I need it bounded.


> Compile error when using generic type with lower bound
> ------------------------------------------------------
>
>                 Key: GROOVY-8034
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8034
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.6
>         Environment: org.codehaus.groovy:groovy-all:2.4.6 in maven project
>            Reporter: Yuriy Kiselev
>
> I'm trying to implement chained generics. It works as Java code but fails to compile by groovyc:
> {code}
> Error:(38, 9) Groovyc: [Static type checking] - Cannot call <C extends com.github.ykiselev.ChainTest.A<? super O, ?>> com.github.ykiselev.ChainTest$A <? super O -> java.lang.Object, ?>#andThen(C) with arguments [com.github.ykiselev.ChainTest$A <Double, String>]
> {code}
> Code (JUnit4 unit test)
> {code}
> package com.github.ykiselev
> import groovy.transform.TypeChecked
> import org.junit.Test
> @TypeChecked
> class ChainTest {
>     static class A<I, O> {
>         def <C extends A<? super O, ?>> C andThen(C next) {
>             return next
>         }
>     }
>     static <I, O, C extends A<? super O, ?>> C chain(A<I, O> self, C next) {
>         self.andThen(next)
>         return next
>     }
>     @Test
>     "should chain"() {
>         def a1 = new A<String, Integer>()
>         def a2 = new A<Integer, Double>()
>         def a3 = new A<Double, String>()
>         def a4 = new A<String, Double>()
>         def a5 = new A<Number, Object>()
>         a1.andThen(a2) // ok
>         a2.andThen(a3) // ok
>         a3.andThen(a4) // ok
>         a4.andThen(a5) // ok (even without "? super O")
>         chain(a1, a2).andThen(a3) // ok
>         chain(chain(chain(chain(a1, a2), a3), a4), a5) // ok
>         a1.andThen(a2)
>                 .andThen(a3) // static type checker error
>                 .andThen(a4)
>                 .andThen(a5)
>     }
> }
> {code}
> This would compile if I change bounded type parameter like this:
> {code}
> def <C extends A<O, ?>> C andThen(C next) {
>             return next
> }
> {code}
> but I need it bounded.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)