Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrictions on Java intersection types with interfaces classes and type variables

Tags:

java

types

jls

Today I tried to write a class with a generic method that uses intersection types and got confused by the different error messages depending on the intersected types. Let's assume that we have an interface and a class and define generic methods in a generic interface:

class ClassType {
}

interface InterfaceType {
}

interface I<T> {
    public <X extends InterfaceType & InterfaceType> void foo();

    public <X extends ClassType & ClassType> void foo1();

    public <X extends ClassType & InterfaceType> void foo2();

    public <X extends InterfaceType & ClassType> void foo3();

    public <X extends T & ClassType> void foo4();

    public <X extends ClassType & T> void foo5();

    public <X extends InterfaceType & T> void foo6();

    public <X extends T & InterfaceType> void foo7();
}

Compiling this produces errors for all method except for public <X extends ClassType & InterfaceType> void foo2();.

Main.java:8: error: repeated interface
    public <X extends InterfaceType & InterfaceType> void foo();
                                      ^
Main.java:10: error: interface expected here
    public <X extends ClassType & ClassType> void foo1();
                                  ^
Main.java:14: error: interface expected here
    public <X extends InterfaceType & ClassType> void foo3();
                                      ^
Main.java:16: error: a type variable may not be followed by other bounds
    public <X extends T & ClassType> void foo4();
                          ^
Main.java:18: error: unexpected type
    public <X extends ClassType & T> void foo5();
                                  ^
  required: class
  found:    type parameter T
  where T is a type-variable:
    T extends Object declared in interface I
Main.java:20: error: unexpected type
    public <X extends InterfaceType & T> void foo6();
                                      ^
  required: class
  found:    type parameter T
  where T is a type-variable:
    T extends Object declared in interface I
Main.java:22: error: a type variable may not be followed by other bounds
    public <X extends T & InterfaceType> void foo7();
                          ^
7 errors

Since intersection should be symmetric, I am surprised that foo2 is accepted but foo3 is rejected. Why is this one case accepted?

I am also wondering in general why there is a distinction between interfaces, classes and type parameters when it comes to intersection types. I can see reasons to not allow more than one class in an intersection type, but the current state accepting ClassType & InterfaceType but not InterfaceType & ClassType seems strangely arbitrary. An intersection A & A is also forbidden, but this is semantically identical to A so I don't see a reason for this.

I am also curious why type variables are not allowed in the intersection. Worst case would be an intersection of two or more classes, but would just be an uninhabited type, so the bottom type.

like image 897
Jens Avatar asked Oct 18 '22 04:10

Jens


1 Answers

You can only have 1 class but have multiple interfaces. If you have a class it must be the first one specified. If you follow this rule, you shouldn't get any compiling errors.

See https://docs.oracle.com/javase/tutorial/java/generics/bounded.html

like image 195
isaace Avatar answered Oct 21 '22 01:10

isaace