java - Valhalla 的值对象可以保存泛型类型并在它们是原始类型时将其扁平化吗?

标签 java generics project-valhalla jep

威尔JEP 169: Value ObjectsJEP 218: Generics over Primitive Types规范可以协同工作吗?

或者更好的是,以下情况可能吗?

@jvm.internal.value.ValueCapableClass
final class Tuple<T1, T2> {

    private final T1 t1;
    private final T2 t2;

    // ...

}

然后

// t1 and t2 flattened because they are ints
final Tuple<int, int> tuple;
<小时/>

我问这个是因为我还没有看到使用泛型类型的 @ValueCapableClass 示例,仅 examples like this one :

@jvm.internal.value.ValueCapableClass
final class MyValue {
    final int x, y;
    // ...
}

我读自 JEP 218: Generics over Primitive Types (强调我的):

Generic type arguments are constrained to extend Object, meaning that they are not compatible with primitive instantiations unless boxing is used, undermining performance. With the possible addition of value types to Java (subject of a separate JEP), this restriction becomes even more burdensome. We propose to remedy this by supporting specialization of generic classes and interfaces when instantiated with primitive type arguments. We propose to remedy this by supporting specialization of generic classes and interfaces when instantiated with primitive type arguments.

With the eight primitive types being the only ones hostile to generics, this is tolerable but annoying; with the advent of value types, this restriction would be far more painful.

但我不清楚它们是否可以一起工作(218169 的扩展),或者通用的 @ValueCapableClasses 可以仅用于非泛型类。

最佳答案

根据this Brian Goetz's talk

[...] they [Value Types] can use generics, they can have type variables [...]

看来泛型正在未来的版本中得到支持。

关于java - Valhalla 的值对象可以保存泛型类型并在它们是原始类型时将其扁平化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169787/

相关文章:

java - Response.sendRedirect() URL 未在浏览器上打开

java - 如何防止命令行 java 进程在 OSX 中窃取焦点?

java - 过滤日志文件

Java 泛型 - 构造函数中的类型不匹配

java - 通用代码中的困惑

泛型类的 Java 8 模糊方法引用

java - 如何使用默认值一般地初始化值类型?

java - 为什么 Java 中的原始数据类型不能是 "null"?