Generics oddness

Kevin Bourrillion kevinb at google.com
Tue May 29 08:44:56 PDT 2012


You might need the <S> parameter that Guava uses anyway -- before we had
it, code like this would not compile:

Comparator<Integer> reverseInts = Ordering.natural().reverse();



On Tue, May 29, 2012 at 8:40 AM, Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> Hi Ben,
> the problem is that there is a clash  between two methods that are
> not-override equivalent; one is
>
> class Comparator {
> [...]
>     Comparator<T> reverse() default {
>         return Collections.reverseOrder(this);
>     }
> [...]
> }
>
>
> The other one is:
>
> class Ordering<T> implements Comparator<T> {
>      public <S extends T> Ordering<S> reverse() {
>         // ...
>     }
> }
>
> As per JLS, this is an illegal override, as Ordering.reverse is not a
> subsignature of Comparator.reverse (as the number of type parameter
> differs). However, I'm investigating as to why the compiler is not
> reporting a clash (as it should).
>
> Thanks
> Maurizio
>
> On 29/05/12 16:07, Ben Evans wrote:
> > public class Ordering<T>  implements Comparator<T>  {
> >      @Override
> >      public int compare(T o1, T o2) {
> >          // ...
> >      }
> >
> >      public<S extends T>  Ordering<S>  reverse() {
> >          // ...
> >      }
> > }
> >
> > public class OrderingMain<T>  {
> >          Ordering<T>  myOrdering;
> >
> >          public Ordering<T>  get() {
> >              return myOrdering.reverse();
> >          }
> > }
>
>
>


-- 
Kevin Bourrillion @ Google
Java Core Libraries Team
http://guava-libraries.googlecode.com


More information about the lambda-dev mailing list