Pair.leftValue() and Pair.rightValue() are too restricted

Issue #52 resolved
John Kozlov created an issue

I would like to sort a list of pairs, but I could not create an Ordering object:

Ordering<Pair<Integer, String>> ordering = Ordering.natural().onResultOf(Pair.<Integer> leftValue());

I think, Pair.leftValue() should have two parameters (A and B) and return Function<Pair<A, B>, A>, not Function<Pair<A, ?>, A>

Comments (3)

  1. Jean-Baptiste Giraudeau

    Ordering<Pair<Integer, ?>> is not good enough for your need I presume? That's the problem with variance annotations: when you start using them, then they must spread everywhere...

  2. Anund McKague

    @orionll is this a 2.x problem? Java 8 solves this with method references.

    Ordering<Pair<Integer, String>> ordering = Ordering.natural().onResultOf(Pair::left);
    

    Interestingly the calling code can decide to forget the second type.

    Function<Pair<Integer, ?>, Integer> ps = Pair::left;
    

    Off the top of my head I'm not sure what changing leftValue from <A, ?> to <A, B> does to binary compatibility. If the addition does not break binary compatibility feel free to submit a PR against 2.6.x for a pre Java 8 release.

  3. Log in to comment