威尔JEP 169: Value Objects和 JEP 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.
但我不清楚它们是否可以一起工作(218
是 169
的扩展),或者通用的 @ValueCapableClasses
可以仅用于非泛型类。
最佳答案
[...] they [Value Types] can use generics, they can have type variables [...]
看来泛型
正在未来的版本中得到支持。
关于java - Valhalla 的值对象可以保存泛型类型并在它们是原始类型时将其扁平化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169787/