You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java泛型方法返回多接口限定类型变量时的编译错误解决求助

Java泛型方法返回多接口限定类型变量时的编译错误解决求助

Apologies for the poor title. I would appreciate some help in crafting a better one, but I don't even know how to describe my problem.


Let's say that I have the following types.

interface M<K, V> {}
interface F<I, O> {}

And let's say that I make a class that implements both of these interfaces.

record A<A1, A2> (A1 a1, A2 a2)
    implements 
        M<A1, A2>, 
        F<A1, A2> 
{}

Ok, cool. I can now use A anywhere that I otherwise would have used M or F.

Now here is my problem. I am trying to do the following, and failing with compiler errors.

public class DoublyNestedGenerics
{

   interface M<K, V> {}
   
   interface F<I, O> {}
   
   record A <A1, A2> (A1 a1, A2 a2)
      implements 
         M<A1, A2>, 
         F<A1, A2> 
   {}
   
   private 
      static 
      <
         D1, 
         D2, 
         D extends 
            M<D1, D2> 
            & F<D1, D2>
      >
      D
      canItWork(final D1 d1, final D2 d2)
   {
   
      // return new A(d1, d2);
      throw new UnsupportedOperationException();
   
   }

}

Ok, so everything compiles. But if I uncomment the return and replace the exception, I get the following compilation error.

DoublyNestedGenerics.java:28: warning: [rawtypes] found raw type: A
      return new A(d1, d2);
                 ^
  missing type arguments for generic class A<A1,A2>
  where A1,A2 are type-variables:
    A1 extends Object declared in record A
    A2 extends Object declared in record A

Ok, makes sense. Let me change the return to be this instead.

return new A<>(d1, d2);

Pressing compile gives me the following compiler error.

DoublyNestedGenerics.java:28: error: incompatible types: cannot infer type arguments for A<>
      return new A<>(d1, d2);
                  ^
    reason: no instance(s) of type variable(s) A1,A2 exist so that A<A1,A2> conforms to D
  where A1,A2,D,D1,D2 are type-variables:
    A1 extends Object declared in record A
    A2 extends Object declared in record A
    D extends M<D1,D2>,F<D1,D2> declared in method <D1,D2,D>canItWork(D1,D2)
    D1 extends Object declared in method <D1,D2,D>canItWork(D1,D2)
    D2 extends Object declared in method <D1,D2,D>canItWork(D1,D2)
1 error

I did not fully understand the error, so I decided to simplify the problem.

I changed the method to instead use some hardcoded types, instead of just D1 and D2. Here is what the new method looks like.

private 
      static 
      <
         D extends 
            M<Integer, Integer> 
            & F<Integer, Integer>
      >
      D
      canItWorkAttempt2(final Integer d1, final Integer d2)
   {
   
      // return new A<>(d1, d2);
      throw new UnsupportedOperationException();
   
   }

Swapping out the comments and pressing compile gave me the following compiler error.

DoublyNestedGenerics.java:44: error: incompatible types: cannot infer type arguments for A<>
      return new A<>(d1, d2);
                  ^
    reason: no instance(s) of type variable(s) A1,A2 exist so that A<A1,A2> conforms to D
  where A1,A2,D are type-variables:
    A1 extends Object declared in record A
    A2 extends Object declared in record A
    D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer)
1 error

Ok, much smaller, and therefore, easier to parse.

One thing that stood out to me was that it said A1 extends Object declared in record A.

Well that's not right -- it should be Integer, not Object. Maybe the inference need some help. So, I changed the return to be this instead.

return new A<Integer, Integer>(d1, d2);

Which resulted in the following compiler error.

DoublyNestedGenerics.java:44: error: incompatible types: A<Integer,Integer> cannot be converted to D
      return new A<Integer, Integer>(d1, d2);
             ^
  where D is a type-variable:
    D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer)
1 error

Progress! I now started to have suspicions, and to confirm them, I decided to simplify my problem significantly. I created the following class.

record N (Integer n1, Integer n2)
    implements 
        M<Integer, Integer>,
        F<Integer, Integer>
{}

Compiled with no issues. Beautiful, let's try to change the return type to use this instead. Here is what I changed the return to.

return new N(d1, d2);

With high hopes, I pressed compile.

DoublyNestedGenerics.java:50: error: incompatible types: N cannot be converted to D
      return new N(d1, d2);
             ^
  where D is a type-variable:
    D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer)
1 error

Very disappointing. But something else popped out to me. In the error, it said where D is a type-variable: D extends M<Integer,Integer>,F<Integer,Integer>.

They used the word extends. Out of desperation, I tried to add the following types.

interface C<C1, C2> extends M<C1, C2>, F<C1, C2> {}

record N2 (Integer n1, Integer n2) implements C<Integer, Integer> {}

And then, from there, I changed my return to say this.

final C<Integer, Integer> blah = new N2(d1, d2);
return blah;

Then I pressed compile.

DoublyNestedGenerics.java:55: error: incompatible types: C<Integer,Integer> cannot be converted to D
      return blah;
             ^
  where D is a type-variable:
    D extends M<Integer,Integer>,F<Integer,Integer> declared in method <D>canItWorkAttempt2(Integer,Integer)
1 error

At this point, I am annoyed.

The error message is somehow telling me that C<Integer,Integer> cannot be converted to D. And it also is telling me that D is a type-variable, specifically that D extends M<Integer,Integer>,F<Integer,Integer> WHICH IS EXACTLY WHAT C DOES. And yet, it still does not work?

Which brings me here. What am I missing? And again, would like some help crafting a better title, if anyone is willing to make suggestions.

Finally, here is the code in full, in case it was harder to follow along.

public class DoublyNestedGenerics
{

   interface M<K, V> {}
   
   interface F<I, O> {}
   
   record A <A1, A2> (A1 a1, A2 a2)
      implements 
         M<A1, A2>, 
         F<A1, A2> 
   {}
   
   record N (Integer n1, Integer n2)
      implements 
         M<Integer, Integer>,
         F<Integer, Integer>
   {}
   
   interface C<C1, C2> extends M<C1, C2>, F<C1, C2> {}
   
   record N2 (Integer n1, Integer n2) implements C<Integer, Integer> {}
   
   private 
      static 
      <
         D1, 
         D2, 
         D extends 
            M<D1, D2> 
            & F<D1, D2>
      >
      D
      canItWork(final D1 d1, final D2 d2)
   {
   
      // return new A<>(d1, d2);
      throw new UnsupportedOperationException();
   
   }

   private 
      static 
      <
         D extends 
            M<Integer, Integer> 
            & F<Integer, Integer>
      >
      D
      canItWorkAttempt2(final Integer d1, final Integer d2)
   {
   
      final C<Integer, Integer> blah = new N2(d1, d2);
      return blah;
      // return new N(d1, d2);
      // throw new UnsupportedOperationException();
   
   }

}

备注:内容来源于stack exchange,提问作者davidalayachew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 09:42:58