JEP 218: Generics over Primitive Types

Brian Goetz brian.goetz at oracle.com
Sun Oct 26 22:41:49 UTC 2014


We definitely need to address this, and actually quite soon.

It is actually much more of an issue for functional interface types 
(Consumer, Function) than for List or Vector, since both List and Vector 
have both in- and out- uses of its type arguments, whereas most 
arguments to functional interfaces do have a natural variance, and 
properly-written codebases are full of variant uses of these.

For example, in Stream, we have to deal with methods like:

     filter(Predicate<? super T>)

The good news is that we've looked at this, and it appears reasonably 
tractable.  We have existing rules for reasoning about wildcards; these 
are likely to be largely unchanged, and extend naturally to value types 
as well (with some small adjustments, such as teaching inference that 
there is no useful variance to be extracted from values during 
inference.)  When we get to the part where we are ready to emit actual 
classfile signatures, we can reason in parts:

  - If T is a reference type, we do the same thing we always did.
  - If T is a value type, then we collapse wildcards to an invariant T.

The logic for this is divided across the compiler and the runtime 
specializer.

There are a few small warts here (JLS defines int <: long (in hindsight, 
this should have been a conversion), but we don't actually want ? 
extends T to contain "long" when T=int); this is a fairly easily removed 
wart, since the subtyping is only used in method overload selection, and 
this can be adjusted to use equivalent conversions.

So, yes, we're working on it.



On 10/24/2014 11:02 PM, Simon Ochsenreither wrote:
> One thing that's still unclear to me is how variance well be handled.
>
> In current Java, it would be possible to say "variance is declared at
> use-site, so we can just reject such variance annotations when the type
> participating is a value type", but in other languages, this is an issue
> because of variance is declared at declaration-site.
>
> Rejecting all such definitions is a bad idea, because that would mean a
> large percentage of such types could either not be specialized at all,
> or wouldn't be able to be variant anymore, including for reference types.
>
> Given that even Java has now a JEP Draft¹ to add declaration-site
> variance, I think it would be a reasonable idea to figure this out
> before the issue arises.
>
> It would probably make sense to allow such declarations, but specify
> that all variance relations where a value type participates are
> collapsed into an invariant definition.
>
> It is not an obscure issue: This question arises for popular types like
> List[T] or Vector[T].
>
> ¹ http://openjdk.java.net/jeps/8043488


More information about the valhalla-dev mailing list