Java generics parameter bounding to any of a range of types

后端 未结 5 1518
一向
一向 2020-12-10 12:26

Is there a syntax or workaround to constrain a generic type parameter to any of a range of types?

I am aware that you can constrain a type to be all

相关标签:
5条回答
  • 2020-12-10 12:57

    Using of instanceof is considered as not very good style of programming, and allowing you to use OR in generics implies you will use one.

    0 讨论(0)
  • 2020-12-10 13:00

    No. It wouldn't make any sense unless all the types had a non-empty union type, e.g. an interface they all implemented, or a base class they all extended, in which case you just specify the union type.

    0 讨论(0)
  • 2020-12-10 13:07
    public class MyWrapper<T extends A | B> {}
    

    You can't do this for interfaces that you don't have control over, but for your own stuff you could use an empty marker interface:

    interface AOrB {
    }
    
    interface A extends AOrB {
        someMethodHere();
    }
    
    interface B extends AOrB {
        someOtherMethodHere();
    }
    
    public class MyClass<T extends AOrB> {}
    

    Regardless of what purists say, using instanceof is perfectly fine when you need it.

    0 讨论(0)
  • 2020-12-10 13:14

    The following code would do the same thing as in the provided example, but without runtime type checking and typecasts.

    public boolean sameAs(MyClass obj) {
        return this.id.equals(obj.id);
    }
    public boolean sameAs(String obj) {
        return this.id.equals(obj);
    }
    

    NPE checking might be a good idea.

    0 讨论(0)
  • 2020-12-10 13:16

    While Java has limited support for "intersection types" like T1 & T2, support for "union types" is scanty.

    Generic types with wildcard are actually union types: G<? extends X> is the union of all G<S> where S is a subtype of X.

    No support for union of arbitrary two types.

    Java 7's multi-catch syntax looks like it supports union of arbitrary exception types

    catch (IOException|SQLException ex){ .. }
    

    but not really, the type of ex is a fixed super class, not a union of the two classes.

    0 讨论(0)
提交回复
热议问题