0

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.

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • 1
    You could ask on the mailing list at valhalla-dev@openjdk.java.net about the future plans. Afaik it's not possible in the current prototype. Currently the main effort seems to be focused on figuring out the right migration path between value and reference types. – Jorn Vernee Sep 05 '18 at 13:15
  • To clarify my earlier comment; it is possible to have generic value types, i.e. `Tuple`, using your example (but using the `__ByValue` keyword from the latest `lworld` build I have, instead of the annotation). It is not possible in that same build to have generics over value types, i.e. `Tuple`. I have seen demos of `ArrayList` in the past, so it seems to be a long term goal, but it's not currently what's being worked on. Though I encourage you to ask on the mailing list to get a better answer to your question. – Jorn Vernee Sep 05 '18 at 18:09

1 Answers1

1

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.

payloc91
  • 3,724
  • 1
  • 17
  • 45