GlenKPeterson / Paguro

Generic, Null-safe, Immutable Collections and Functional Transformations for the JVM
Other
312 stars 24 forks source link

Allow passing Entry subclasses to Map factory methods #51

Open kilink opened 1 year ago

kilink commented 1 year ago

Change the factory methods to accept a covariant type (e.g., Iterable<? extends Map.Entry>), which allows passing Iterables of UnEntry.

Note: perhaps I'm just using the library wrong, but I ran into this issue when I was attempting to merge two maps. It seemed to me that I should be able to do this:

PersistentTreeMap.of(map.concat(map2));

While I know it's doable like so, to me it seems like an unnecessary limitation, and this form is more verbose:

map.concat(map2).toImSortedMap(Equator.defaultComparator(), entry -> entry);

(Would be happy to be shown a better way to merge two maps).

Another consequence of making it covariant is that you can now pass a Persistent map instance to the factory method.

GlenKPeterson commented 1 year ago

Thank you. What you're asking for seems to make sense. I appreciate that you matched the code style and added unit tests. I'm sorry to take so long to even start looking at this.

I had a few nagging memories that turned out to be red herrings. The one that I'm still concerned about is type inference in Java. It should probably all be OK though. I need more time to try it out on my machine before merging, but at this point, I do plan to merge your changes.