List of all default methods
Brian Goetz
brian.goetz at oracle.com
Thu Nov 21 06:56:39 PST 2013
> from what I can see Groovy has only this one conflict of
> Iterable#sort(Comparable) from us with List#sort(Comparable) from jdk8.
> Is there any rationale behind adding a mutating sort method to List by
> default?
Yes! All the other methods on List (add, remove, retainAll) are
in-place mutative. The standard way to sort a List is
Collections.sort(List), which is also in-place (and less efficient).
The new method on List is simply Collections.sort(List), but in the
right place, and can be optimized by implementations.
All the new methods which produce lazy / functional views are on Stream.
So you can do
list.stream().sorted()
to produce a new sorted stream. To avoid making things more confusing,
we followed the guideline of adding in-place mutative methods to the
concrete collections, and adding functional / lazy methods to Stream,
rather than mixing.
BTW, adding stuff to Iterable is dangerous, as you've seen. And you
wouldn't even need default methods for that to happen; if we'd not done
default methods at all, but added this sort method to ArrayList, you'd
have the exact same conflict.
This worked as long as those JDK guys were asleep at the switch, and
never updated the libraries. But those days are over!
I think you've got a few choices here:
- update your libraries
- modify your compiler to resolve these sorts of conflicts.
For example, now that List has a conflict between
void sort(Comparator)
and
List sort(Comparator)
you could use target-typing in your method overload selection to pick
one of these, rather than reporting a conflict.
As a general rule, adding stuff to Iterable is probably going to cause
ongoing pain. Use with care.
More information about the lambda-dev
mailing list