Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JDK 21 Sequenced Collections #6903

Open
4 tasks done
rhys-saldanha opened this issue Jan 6, 2024 · 1 comment
Open
4 tasks done

Support JDK 21 Sequenced Collections #6903

rhys-saldanha opened this issue Jan 6, 2024 · 1 comment

Comments

@rhys-saldanha
Copy link

rhys-saldanha commented Jan 6, 2024

1. What are you trying to do?

Please see JEP-431 for background on what a Sequenced Collection is.

The javadoc for ImmutableCollection mentions that most Guava Collections have a well-defined iteration order. Indeed, I often rely on this behaviour. This issue is to discuss support for the SequencedCollections interface, in order to better communicate that Guava Collections have this property.

2. What's the best code you can write to accomplish that without the new feature?

This example does not make much sense without the context of the added methods in SequencedCollection, however:

final var foo = ImmutableSet.of(1,2,3);
foo.asList().get(0); // = 1

3. What would that same code look like if we added your feature?

Sequenced Collections add new convenience methods that arise from having a defined iteration order. Applying this to (for example) ImmutableSet, which preserves insertion order, would result in the following code being valid:

final var foo = ImmutableSet.of(1,2,3);
foo.getFirst(); // = 1

(Optional) What would the method signatures for your feature look like?

No response

Concrete Use Cases

I have not yet used Guava with JDK 21.

Please see related Guava discussion: https://groups.google.com/g/guava-discuss/c/IXWZCHn7yJs

Packages

com.google.common.collect

Checklist

@rhys-saldanha rhys-saldanha added the type=addition A new feature label Jan 6, 2024
@cpovirk
Copy link
Member

cpovirk commented Jan 6, 2024

One thing that Rhys mentioned that I hadn't considered before was the documentation aspect: For someone new to, say, ImmutableMap, it could be useful to see that it's an implementation of SequencedMap. While we try to drive home the benefits of our immutable collections, a page like https://guava.dev/ImmutableMap doesn't mention ordering until you follow a link or at least read about a factory method like of.

Then there's the benefit of the methods themselves, like getFirst, as mentioned above. We could add those without worrying about some of the complexities that come with implementing the interface. That said, as discussed in the link above, that still leaves a couple challenges. I think the could both be solved by using --release 8, but that requires dealing with Unsafe and other unavailable APIs. --release 8 has a pretty good chance of becoming important someday (for this and other reasons), so we should reevaluate the SequencedCollection methods at that point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants