Thanks for the response @Stuart Marks <[email protected]>! > A third concern is that people come along and ask whether we can > have something in Java that's rather like Python's slices.
Hah, this email started out as a request for Python slices lol. But I came to the same conclusion as you. I am quite curious about what Project Amber has to say about this. Range patterns, for example. > You had asked about List, not String, but I think similar issues apply. > I'd guess that String and substring operations are a lot more frequent > than subList operations. But maybe you have some subList use cases in > mind. Could you give more details about what you're thinking of? I definitely agree that there is heavy overlap in the problem spaces of subList and substring. As for use cases, mostly Path-Finding algorithms and NLP <https://en.wikipedia.org/wiki/Natural_language_processing> work. The PFA is mostly self explanatory, but for NLP, I splice up Strings in a bunch of different ways. You have to cycle through different permutations of word groupings to anchor a phrase to a concept (so I get to use both substring and subList lol). And each concept has an associated type for it -- a permitted subtype of a sealed type (which may itself be another permitted subtype of another). Since most of this is me trimming from the front or end, it became way more manageable to make headList and subList for myself. So, I think that they could be useful for others too. I will say -- zooming out (while thinking of the substring/subList duality), String and List are just Sequences of X. Maybe there is value in making something more broad here? Maybe some Sequence-like interface where we expose some helpful range methods? It would all devolve to the statement forms you mentioned earlier. And in an ideal world, Project Amber could latch onto that if/when range patterns go live. I remember watching this video by Brian Goetz about Growing the Language <https://www.youtube.com/watch?v=Gz7Or9C0TpM>. It kind of feels like an interface like this would be very amenable, though I might also be thinking too far ahead lol. On Wed, Nov 19, 2025 at 12:48 AM Stuart Marks <[email protected]> wrote: > > > On 11/16/25 10:51 AM, David Alayachew wrote: > > Could we add headList(int) and tailList(int) to j.u.List? I searched JBS > and found > > nothing. > > > > It's commonly what people want when doing subList(int, int), so this > should be > > pretty well received. > > Maybe. :-) > > There's a nexus of diffuse concerns here. > > One concern is subranges of things in general, such as Strings, > CharSequences, > Lists, and arrays. Subranges are all expressed as methods with (start, > end) > parameters. These are powerful enough to do anything you need to do, but > they're > sometimes inconvenient. > > One reason these can be inconvenient is related to the second concern, > which is that > sometimes the APIs require the creation of a local variable, which in turn > forces an > expression to turn into a statement. For example, consider a task of > getting a > String from somewhere, taking the first three characters, and passing that > elsewhere > to perform further work. One can write that as > > furtherWork(getString().substring(0, 3)); > > But if you want to take the *last* three characters and pass them along, > you have to > do this: > > var tmp = getString(); > furtherWork(tmp.substring(tmp.length() - 3)); > > This isn't terrible, but sometimes it disrupts an expression to declare a > local > variable. > > At least there is a one-arg substring method that takes characters to the > end of the > string. If this method weren't there, you'd have to call length() twice > (or store > the length in another local variable). Again, not terrible, but yet > another thing > that adds friction. > > A third concern is that people come along and ask whether we can have > something in > Java that's rather like Python's slices. For a variety of reasons I don't > think we > should accept negative indexes or step values other than 1, but there are > some > things that probably occur frequently. For example, to get the last three > characters > of a string, one can write s[-3:]. That's pretty nice. > > You had asked about List, not String, but I think similar issues apply. > I'd guess > that String and substring operations are a lot more frequent than subList > operations. But maybe you have some subList use cases in mind. Could you > give more > details about what you're thinking of? > > > In that case, would be nice if we could add an entry to JBS with a Won't > Fix, to > > make it easier for those looking to see why not. Maybe even link this > thread for > > further reading. > > Heh, yeah I've done that a couple times -- filed a bug just to close it > out with an > explanation why. I was wondering whether anybody would find that useful. I > think > this topic is worth some discussion, though, so I won't do that quite yet. > :-) > > s'marks > >
