Incompatible Types java generics issue -


i cannot understand incompatible types compilation error.

interface request {}  interface response {}  class foo {     <rq extends request, rs extends response> rs foo(rq rq) {       // doesn't compile: incompatible types error       return foo3(foo2(rq));        /* compiles fine */        // rs rs = foo2(rq);       // return foo3(rs);    }     <rs extends response, rq extends request> rs foo2(rq rq) {       return null;    }     <rs extends response> rs foo3(rs rs) {       return rs;    } 

the curious thing me code compiles fine if in method foo replace this:

return foo3(foo2(rq)); 

by:

rs rs = foo2(rq); return foo3(rs); 

for sure i'm missing something, still don't why.

thanks help.

that's because in case of using

rs rs = foo2(rq); return foo3(rs); 

the compiler know concrete type (rs) use specialization of foo2. telling type want.

in case of foo3(foo2(rq)); compiler cannot deduce that. less specialized type response result of foo2, doesn't work foo3.

note automatic deduction necessary because type parameters rs independent every method. nobody guarantees refer same type in whole class. if want them same in whole class, make class generic, not methods.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -