New JEP: Concise Method Bodies
kevinb at google.com
Tue Oct 9 19:27:07 UTC 2018
On Tue, Oct 9, 2018 at 12:15 PM <forax at univ-mlv.fr> wrote:
> We have a rich API for building Comparators. I would expect most
> non-legacy implementations of Comparable to implement their 'compareTo'
> method by delegating to a Comparator. And you don't want to build the
> Comparator each time the 'compareTo' method gets called, so you'll probably
> stash it in a field.
> I'm curious about what you'd expect someone to put inside their
> 'compareTo' method body, if not an invocation of Comparator.compare. (Of
> course they can roll their own int-fiddling logic, but why when the
> Comparator API is sitting right there?)
> I don't expect a lot of people to write their own compareTo because not a
> lot of classes have a natural order
Ratio of Comparator to Comparable implementations in our codebase seems to
be less than 3:1. That's not that big a difference imho.
> (that the reason why there is no support of Comparable on a record, no ?)
I don't think so. I think the reason is that the default behavior we'd have
to use is regularly enough not what the user wants. And you don't want
program behavior to change when fields are reordered in the source file.
imho, the Comparator construction methods added in 8 are such an ideal way
to configure comparison behavior that a language feature really has no way
to beat them.
Kevin Bourrillion | Java Librarian | Google, Inc. | kevinb at google.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the amber-spec-experts