简体   繁体   English

Java中的T(泛型类型)实例

[英]Instance of T (generic type) in Java

In short: Why cannot I write the following code in Java? 简而言之:为什么我不能用Java编写以下代码?

public class Foo<T> {
    public void foo(Object bar) {
        if (bar instanceof T) {
            // todo
        }
    }
}

Yeah, I know, the generics is kinda hacked into Java. 是的,我知道,仿制药有点被Java攻击。 Generics wasn't there until Java 1.5, and the generic type is lost during runtime. 在Java 1.5之前,泛型不存在,并且泛型类型在运行时丢失。

I also know, there are some patterns for it. 我也知道,它有一些模式。 For example: 例如:

public class Foo<T> {

    Class<T> clazz;

    public Foo(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void foo(Object bar) {
        if (clazz.isInstance(bar)) {
            // todo
        }
    }
}

My question is, why isn't it automatically done by the compiler? 我的问题是,为什么它不是由编译器自动完成的?

For each class, where any generic type is present, the compiler could automatically add one (or more, if I have more generic types) parameter for each constructor, and bind those values to private fields. 对于存在任何泛型类型的每个类,编译器可以为每个构造函数自动添加一个(或更多,如果我有更多泛型类型)参数,并将这些值绑定到私有字段。 And each time, I write bar instanceof T it could compile this as clazzOfGenericT.isInstance(bar) . 每次,我写bar instanceof T它可以编译为clazzOfGenericT.isInstance(bar)

Is there any reason, this is not implemented? 有什么理由,这没有实现?

I'm not totally sure, this wouldn't break backwards compatibility* - but then, new JVM languages (like Scala, or Kotlin) why doesn't have this feature? 我不完全确定,这不会破坏向后兼容性* - 但是,新的JVM语言(如Scala或Kotlin)为何没有这个功能?

*: IMHO it could be done, without break any backwards compatibility. *:恕我直言,它可以完成,不会有任何向后兼容性。

Proposals for features added to Java are slow-moving and there are higher-priority features. 添加到Java的功能的提议是缓慢的,并且有更高优先级的功能。 "They haven't gotten to it yet." “他们还没有完成它。”

Generics wasn't there until Java 1.5, and the generic type is lost during runtime. 在Java 1.5之前,泛型不存在,并且泛型类型在运行时丢失。

... ...

My question is, why isn't it automatically done by the compiler? 我的问题是,为什么它不是由编译器自动完成的?

For each class, where any generic type is present, the compiler could automatically add one (or more, if I have more generic types) parameter for each constructor, and bind those values to private fields. 对于存在任何泛型类型的每个类,编译器可以为每个构造函数自动添加一个(或更多,如果我有更多泛型类型)参数,并将这些值绑定到私有字段。

Well, now, here you're just asking why Java doesn't store generic type information for run-time. 那么,现在,您只是在问为什么Java不存储运行时的泛型类型信息。 You're asking for reification. 你要求具体化。 The answer is that Java's generics are implemented with erasure, which you already know. 答案是Java的泛型是用擦除实现的,你已经知道了。

Yes, reification is possible and other languages do it. 是的,可以进行具体化,其他语言也可以。 Yes, maybe Java will do it someday too. 是的,也许Java有一天也会这样做。 Maybe they will do it in a way similar to what you've suggested. 也许他们会以类似于你建议的方式做到这一点。 Or maybe not. 或者可能不是。

Something like this could eventually be addressed by Project Valhalla . 瓦尔哈拉计划最终可以解决这样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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