Can Valhalla's Value Objects hold generic types and flatten them in case they are primitive types?

433 views Asked by At

Will JEP 169: Value Objects and JEP 218: Generics over Primitive Types specifications work together?

Or better, is the following scenario possible?

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

    private final T1 t1;
    private final T2 t2;

    // ...

}

and then

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

I'm asking this because I haven't seen an example of a @ValueCapableClass using generic types, only examples like this one:

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

I read from JEP 218: Generics over Primitive Types (emphasis mine):

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.

and

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.

but it's unclear to me whether they are meant to work together (218 is an extension of 169), or generic @ValueCapableClasses can be used only for non-generic classes.

1

There are 1 answers

0
payloc91 On

According to this Brian Goetz's talk

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

So it seems generics are in the process of being supported in future builds.