Factory methods for SequencedSet and SequencedMap
forax at univ-mlv.fr
forax at univ-mlv.fr
Sat Jan 25 13:01:53 UTC 2025
> From: "Rafael Winterhalter" <rafael.wth at gmail.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "core-libs-dev" <core-libs-dev at openjdk.org>, "joe darcy"
> <joe.darcy at oracle.com>
> Sent: Tuesday, January 21, 2025 10:17:35 AM
> Subject: Re: Factory methods for SequencedSet and SequencedMap
> Wouldn't this already be possible with today's union types?
> static <E, S extends Set<E> & SequencedSet<E>> S of(E... elements) { ... }
No, because when you have a type variable declared on a method, it means \forall,
so here, the method of() has to work for all implementations of S, so all implementations of a SeqenceSet.
To see the issue, you can fix the type argument, by example
TreeSet<String> set = Set.<String, TreeSet<String>>of("foo")
is obviously wrong but allowed by the declaration.
> Then again, I do not think that the regular Set and Map implementations should
> be sequenced, mainly to avoid that tests suffer from this sequencing.
I do not disagree :)
Rémi
> Am So., 19. Jan. 2025 um 16:18 Uhr schrieb Remi Forax < [
> mailto:forax at univ-mlv.fr | forax at univ-mlv.fr ] >:
>> What can be done is to have Set.of()/Map.of() to delegate to
>> SequenceSet.of()/SequenceMap.of() so there is only one implementation at
>> runtime.
>> Also, technically, there is a way to change the return type in a binary
>> compatible way ... if union types are supported in the language.
>> In that case, Set.of() can be declared like this
>> static <E> Set<E> | SequenceSet<E> of(E... ) {
>> ...
>> }
>> because of erasure, the binary descriptor will use Set, but the Signature will
>> be Set<E> | SequenceSet<E> so the compiler will see the return type as a
>> SequencedSet .
>> Obviously, union types is a far bigger features than adding
>> SequenceSet/SequenceMap so it's a big if, but it may happen in the future.
>> Rémi
>>> From: "joe darcy" < [ mailto:joe.darcy at oracle.com | joe.darcy at oracle.com ] >
>>> To: "core-libs-dev" < [ mailto:core-libs-dev at openjdk.org |
>>> core-libs-dev at openjdk.org ] >
>>> Sent: Friday, January 17, 2025 6:30:40 PM
>>> Subject: Re: Factory methods for SequencedSet and SequencedMap
>>> On 1/16/2025 11:26 PM, Rafael Winterhalter wrote:
>>>> Would it even be possible to change the return types of Set.of(...) and
>>>> Map.of(...) without breaking binary compatibility?
>>> In short, no.
>> [...]
>>> -Joe
>> Rémi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250125/79a5c11c/attachment.htm>
More information about the core-libs-dev
mailing list