Range API
Archie Cobbs
archie.cobbs at gmail.com
Mon Sep 23 02:10:29 UTC 2024
On Sun, Sep 22, 2024 at 2:03 PM Olexandr Rotan <rotanolexandr842 at gmail.com>
wrote:
> Hello everyone! I am writing here today to invite everyone to participate
> in the discussion regarding the Range APi proposal I have made into JDK.
>
A few comments...
Guava has addressed this same problem; their approach worth reviewing:
https://github.com/google/guava/wiki/RangesExplained
You are restricting to domains that implement Comparable, but this limits
usefulness. For example, byte[] arrays are often ordered lexicographically
as unsigned values and we want to work with ranges of byte[] arrays. It
would be more general to allow supplying a Comparator (if needed, otherwise
null). Then you would also want to expose this using a comparator() method
(like SortedSet does). Note that Guava does not allow providing a
Comparator so they voted the other way on this.
The start() and end() be should be specifiable as either inclusive or
exclusive, otherwise open bounds are not possible. For example, how would
one express the range of all Strings strictly less than "zzz"? (note every
element in the infinite sequence "zzy", "zzyy", "zzyyy", ... is less than
"zzz"). This is why for example NavigableSet.subSet() has parameters for
that.
I've never encountered a negative range and am skeptical that it would
actually be useful. What's an example of a real-world use case? I don't see
how they would help diagrams visualization other than maybe saving you a
boolean, i.e., you could just use the normal range and draw the arrow the
other way.
In my experience, when you work with ranges you often also need to work
with sets of ranges. If we had a Range class then it would be natural to
also have a RangeSet class as well with union, intersection, difference,
and ordered iteration operations, etc.
-Archie
--
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240922/35e2733c/attachment.htm>
More information about the core-libs-dev
mailing list