Introduce Covariance/Contravariance at declaration site for Java 9 ? (original) (raw)

Neal Gafter neal at gafter.com
Thu Nov 8 22:00:16 PST 2012


On Mon, Oct 15, 2012 at 5:24 PM, Joshua Bloch <josh at bloch.us> wrote:

I believe that declaration site variance annotations are every bit as bad as use-site annotations. They're bad in a different way--they force you to write idiosyncratic types because natural types don't lend themselves to fixed variance restrictions--but they're still bad.

Both use-site variance and declaration-site variance require writing "idiosyncratic" type declarations, because what would be natural in a world with only one kind of variance isn't the same as what would be natural with only the other. Declaration-site variance feels very natural in languages that support it - in my experience more natural than pure use-site variance.

The mix that Remi suggests, where functional interfaces are automatically variant, is a useful middle ground that was investigated with BGGA (using the function type syntax). Because functional interfaces are just interfaces that happen to have a single method, one would expect the inferred variance to work for all interfaces (not just single-method interfaces). That gives you the full generality of use-site variance (but, like C#, for interfaces only, not classes).

This would have to be done very carefully, though. It is not strictly compatible with the existing language (even when restricted to SAM interfaces) because it introduces new edges in the subtype lattice for existing types (e.g. from Iterable to Iterable).

Cheers, Neal



More information about the lambda-libs-spec-observers mailing list