Java中的通用类型推断限制 [英] Generic type inference limits in Java

查看:55
本文介绍了Java中的通用类型推断限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java泛型类型推断项目中,我面临以下问题.这是与我的原始代码示例相似的代码示例:

I'm facing the following problem in my project with Java generics type inference. This is a code sample that's similar to my original one:

public class BuildableObject<R, S> {
  public static class OneParameter<R> { }
  public static class TwoParameters<R, S> { }
  interface TwoParamInterface<R, S> { }
  public static class Implementer<T> implements TwoParamInterface<T, T> {}

  private final OneParameter<R> first;
  private final OneParameter<S> second;
  private final TwoParameters<R, S> third;
  private final TwoParamInterface<R, S> fourth;

  private BuildableObject(OneParameter<R> first, OneParameter<S> second, TwoParameters<R, S> third, TwoParamInterface<R, S> fourth) {
    this.first = first;
    this.second = second;
    this.third = third;
    this.fourth = fourth;
  }

  public static class Builder<R, S> {
    private OneParameter<R> first = null;
    private OneParameter<S> second = null;
    private TwoParameters<R, S> third = null;
    private TwoParamInterface<R, S> fourth = null;

    public Builder() {}

    public Builder<R, S> first(OneParameter<R> first) {
      this.first = first; return this;
    }

    public Builder<R, S> second(OneParameter<S> second) {
      this.second = second; return this;
    }

    public Builder<R, S> third(TwoParameters<R, S> third) {
      this.third = third; return this;
    }

    public Builder<R, S> fourth(TwoParamInterface<R, S> fourth) {
      this.fourth = fourth; return this;
    }

    public BuildableObject<R, S> build() {
      return new BuildableObject<>(first, second, third, fourth);
    }
  }

  public static void main(String... args) {
    new Builder<>()
        .first(new OneParameter<>())
        .second(new OneParameter<>())
        .third(new TwoParameters<>())
        .fourth(new Implementer<String>())
        .build();
  }
}

此代码在new Implementer<String>处中断,但如果我使用new Builder<String, String>而不是new Builder<>,则可以使用.

This code breaks at new Implementer<String>, but works if I use new Builder<String, String> instead of new Builder<>.

如果在new Implementer<String>中指定了R和S的类型,为什么Java不能推断Builder的类型是Builder<String, String>?

Why can't Java infer that the type of the Builder is Builder<String, String> if the types of R and S are specified in new Implementer<String>?

Java泛型类型推断的局限性是什么?它仅解析构造函数或静态方法中提供的类型吗?我还没有找到任何文档.

What are the limits of Java generic types inference? Does it only resolve types provided in constructors or static methods? I haven't found any documentation on this.

这是否意味着如果我们不能使用类型推断,那么此类可能不是类型安全的吗?

Does this mean in any way that this class might not be type safe if we can't use type inference?

推荐答案

It's documented in detail in https://docs.oracle.com/javase/specs/jls/se9/html/jls-18.html. But the problem is that it's documented in detail: there is a lot of jargon you are unlikely to be familiar with unless you read papers on this subject.

在这种情况下,您只需要了解对于类型推断,在new Builder<>()之后调用哪种方法都没有关系.仅使用构造函数本身的参数(以及例如Builder<String, String> b = new Builder<>();中的目标类型,但在这种情况下您没有一个).

For this case you just need to understand that for type inference it doesn't matter what methods you call after new Builder<>(); only parameters to the constructor itself are used (and a target type e.g. in Builder<String, String> b = new Builder<>();, but in this case you don't have one).

它仅解析构造函数或静态方法中提供的类型吗?

Does it only resolve types provided in constructors or static methods?

否.

这是否意味着如果我们不能使用类型推断,那么此类可能不是类型安全的吗?

Does this mean in any way that this class might not be type safe if we can't use type inference?

它们是完全无关的.

这篇关于Java中的通用类型推断限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆