Strange behaviour of generics in Java?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
20 views
Wrote some generic tin, and then, suddenly refused to compile Java code, although it would seem everything is fine.


In short, what happens here


Transformable — a parameterized interface, parameterized it in order to implementing classes to indicate the type of the result of the transformation.


Test method imposes a restriction on the type of the transferred object that human language can be translated as "an input object contains the transform method with a return value of type "a subtype I"


So transform () will return an object compatible with the type I. And the returned object can again call the method transform ().


If I do it through intermediate variables, then it works, but one line is not compiled, although the essence is the same.


What's wrong?

 class Main {     /** * @param <T> * transformation result type *  */    public interface Transformable<T extends Transformable<?>>  { T transform();    }     <I extends Transformable<?  extends I>> I test(final I input) {         final I ans = input.transform().transform(); // can't compile!         // final I ans1 = input.transform();        // final I ans = ans1.transform(); // works!         return ans;    } }  


______________________
by | 20 views

2 Answers

0 like 0 dislike
What version of JDK?
I javac'om 6u20 compiled both.
by
0 like 0 dislike
So it should work ;)
\r
final I ans = (I)input.transform().transform();
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Apr 12, 2019 by synapse_people
0 like 0 dislike
1 answer
asked Mar 23, 2019 by second_pilot
0 like 0 dislike
7 answers
asked Apr 2, 2019 by ali_aliev
0 like 0 dislike
4 answers
asked Mar 25, 2019 by GORKOFF
110,608 questions
257,186 answers
0 comments
26,933 users