Generics oddness

Peter Levart peter.levart at marand.si
Wed May 30 04:28:29 PDT 2012


On Tuesday, May 29, 2012 08:44:56 AM Kevin Bourrillion wrote:
> 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();

That's questionable, because it is forgiving to badly written API-s that 
require Comparator<Type> instead of Comparator<? super Type> as a method 
argument type...

Regards, Peter

> 
> 
> 
> 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();
> > >          
> > >          }
> > > 
> > > }


More information about the lambda-dev mailing list