简体   繁体   中英

Can't refer to generic type from bounded wildcard reference

What is wrong with Class A below that won't allow it to compile?

public class GenericsHell {
   interface Shape{} 
   interface Circle extends Shape {} 

   interface ShapeHelper<T extends Shape> {
      void draw(T shape);
   }

   class A<T extends Shape> {
      A(T shape, ShapeHelper<? extends Shape> helper) {
         helper.draw(shape); // Issues not-applicable argument error 
      }
   }

   class B {
      B(Circle circle, ShapeHelper<Circle> helper) {
         helper.draw(circle);
      }
   }
}   

Eclipse gives the following error:

The method draw(capture#1-of ? extends Shape) in the type ShapeHelper<capture#1-of ? extends Shape> is not applicable for the arguments (T)

You defined your generic parameter for Class A as one thing, but then tried to use it in an incompatible fashion in your constructor ( <T extends Shape> is not the same thing as <? extends Shape> . To get your code to compile change it to consistently use your already defined generic parameter:

class A<T extends Shape> {
    public A(T shape, ShapeHelper<T> helper) {
        helper.draw(shape);
    }
}

As an aside, your code doesn't generate the error message you showed in your question. Rather it would be something more like this:

The method draw(capture#1-of ? extends GenericsHell.Shape) in the type GenericsHell.ShapeHelper is not applicable for the arguments (T)

The method draw(capture#1-of ? extends GenericsHell.Shape) in the type GenericsHell.ShapeHelper<capture#1-of ? extends GenericsHell.Shape> is not applicable for the arguments (T)

The problem is that in your declaration, shape is of type T, but you request a ShapeHelper of type <? extends Shape> which means that one could pass as argument a ShapeHelper where S and T are distinct.

You would then call helper<S>.draw(shape<T>); which doesn't make sense.

A correct implementation for this method would be:

class A<T extends Shape> {
  A(T shape, ShapeHelper<T> helper) {
    helper.draw(shape); 
  }
}

Which ensures that the shape and the shape helper are of compatible types.

Would be important to see your call to A . But it seems like you did something like A<Integer> . But T must extend Shape according to your class declaration.. and Integer doesn't. So either change <? extends Shape> <? extends Shape> to <T> or provide a type that is a Shape to A

Try this instead:

class A<T extends Shape> {
    A(T shape, ShapeHelper<T> helper) {
        helper.draw(shape);
    }
}

Remember PECS (producer extends , consumer super ).

helper is a consumer (you pass something to it), hence it cannot be extends . Perhaps it could be super , but I don't know if that makes sense in this case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM