Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind two generic types

I have a question regarding generics. I have the following interfaces:

public interface InterfaceA<B extends InterfaceA.InterfaceB> {

    public interface InterfaceB<A extends InterfaceA> {

        void setA(A a);
    }
}

And the 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 to me, that the call b.setA(this) is unchecked - but I don't like it, so I tried a 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, its clear to me, that the call b.setA((A)this) is an uncheck cast.

But how should this be implemented or redesigned in order to get rid of the unchecked code?

like image 368
Peter Avatar asked Feb 27 '26 01:02

Peter


1 Answers

You are actually having a mutual recursive generic definition that you break by using raw types: in

b.setA((A)this); // <- Unchecked cast

this is of type InterfaceA<? extends InterfaceA.InterfaceB<? extends InterfaceA>>, but it should be of type InterfaceA<? extends InterfaceA.InterfaceB<? extends InterfaceA<? extends InterfaceA.InterfaceB<...>>>>. You would have to use instead

public interface InterfaceA<B extends InterfaceA.InterfaceB<?>> {

    public interface InterfaceB<A extends InterfaceA<B>> { //<- cannot make a static reference to the non-static type B

        void setA(A a);
    }
}

but you cannot use B, which is non-static, in the static interface declaration (interface declarations are always static).


For detail, one further try: Using the 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>, 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 an unchecked cast, since now the nested type parameter of InterfaceA in interface InterfaceB<A extends InterfaceA<? extends InterfaceA.InterfaceB<?>>> is again an arbitrary subclass of InterfaceA.InterfaceB<?>.


Update, since you've asked for a general design:

I would think of InterfaceB (in fact, interfaces in general) as abstraction from a concrete implementation: You only need the interface InterfaceB, not its implementation details, in your implementation of InterfaceA. Think of InterfaceB as a contract, and you do not care about the implementation. Hence there is no need for binding the implementation of InterfaceA to the implementation of InterfaceB:

public interface InterfaceA {

    public interface InterfaceB {

        void setA(InterfaceA a);
    }
}

Only if, for reasons I can't see, you do want to have the same type for all instances of InterfaceB that you are using, you need generics. Vice versa for InterfaceA. With the last generics example above, you can at least fix the types for InterfaceA and InterfaceB, and would only have to dynamically assert that A's B and B's A are the same.

Showing that no type checked solution exists in Java is difficult, but maybe it becomes plausible with the following example, which would be a solution if Java allowed the combination of extends and 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 to think of it, the Pluggable Type System, which adds further typing to Java, allows this combination of extends and super, and might therefore offer a solution to your problem. But I find it too complex for what you get, and would either stick with simply using Interfaces without generics or some unchecked cast.

like image 155
DaveFar Avatar answered Mar 01 '26 14:03

DaveFar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!