java - Bind two generic types -
i have question regarding generics. have following interfaces:
public interface interfacea<b extends interfacea.interfaceb> { public interface interfaceb<a extends interfacea> { void seta(a a); } }
and following abstract implementation of interfacea
:
public abstract class aimplone<b extends interfacea.interfaceb> implements interfacea<b> { private final b b; public aimplone(b b) { this.b = b; b.seta(this); // <-- unchecked call... } }
its clear me, call b.seta(this)
unchecked - don't it, tried second abstract implementation:
public abstract class aimpltwo<a extends interfacea, b extends interfacea.interfaceb<a>> implements interfacea<b> { private final b b; public aimpltwo(b b) { this.b = b; b.seta((a)this); // <-- unchecked cast } }
and again, clear me, call b.seta((a)this)
uncheck cast.
but how should implemented or redesigned in order rid of unchecked code?
you having mutual recursive generic definition break using raw types: in
b.seta((a)this); // <- unchecked cast
this
of type interfacea<? extends interfacea.interfaceb<? extends interfacea>>
, should of type interfacea<? extends interfacea.interfaceb<? extends interfacea<? extends interfacea.interfaceb<...>>>>
. have use instead
public interface interfacea<b extends interfacea.interfaceb<?>> { public interface interfaceb<a extends interfacea<b>> { //<- cannot make static reference non-static type b void seta(a a); } }
but cannot use b
, non-static, in static interface declaration (interface declarations static).
for detail, 1 further try: using alternative
public interface interfacea<b extends interfacea.interfaceb<?>> { public interface interfaceb<a extends interfacea<? extends interfacea.interfaceb<?>>> { void seta(a a); } } abstract class aimpltwo<b extends interfacea.interfaceb<a>, extends interfacea<b>> implements interfacea<b> { private final b b; public aimpltwo(b b) { this.b = b; b.seta((a)this); // <-- unchecked cast } }
causes again unchecked cast, since nested type parameter of interfacea
in interface interfaceb<a extends interfacea<? extends interfacea.interfaceb<?>>>
again arbitrary subclass of interfacea.interfaceb<?>
.
update, since you've asked general design:
i think of interfaceb (in fact, interfaces in general) abstraction concrete implementation: need interface interfaceb, not implementation details, in implementation of interfacea. think of interfaceb contract, , not care implementation. hence there no need binding implementation of interfacea implementation of interfaceb:
public interface interfacea { public interface interfaceb { void seta(interfacea a); } }
only if, reasons can't see, want have same type instances of interfaceb using, need generics. vice versa interfacea. last generics example above, can @ least fix types interfacea , interfaceb, , have dynamically assert a's b , b's same.
showing no type checked solution exists in java difficult, maybe becomes plausible following example, solution if java allowed combination of extends , super:
public interface a<tb extends a.b<?>> { public interface b<ta extends a<? extends a.b<?>>> { void seta(ta a); } } class aimpltwo<tb extends a.b<ta>, ta extends aimpltwo<tb, ta> super aimpltwo<tb, ta>> implements a<tb> { private final tb b; public aimpltwo(tb b) { this.b = b; b.seta((ta)this); } }
...come think of it, pluggable type system, adds further typing java, allows combination of extends , super, , might therefore offer solution problem. find complex get, , either stick using interfaces without generics or unchecked cast.
Comments
Post a Comment