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

Paul King closed GROOVY-8034.
-----------------------------

> 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
>          Components: Static Type Checker
>    Affects Versions: 2.4.6, 2.4.7
>         Environment: groovy JDK 2.4.7, 2.4.6, 
> Java used: jdk1.7.0_51, jdk1.8.0_40, 9-ea+114, 
> RedHat\java-1.8.0-openjdk-1.8.0.111-3
>            Reporter: Yuriy Kiselev
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 4.0.0-alpha-3
>
>
> 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
>     void "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
(v8.3.4#803005)

Reply via email to