You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please consider adding the merge method to mutable.Map. Java defines the method as follows, see java docs
/** If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key.@paramkey - key with which the resulting value is to be associated@paramvalue - the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key@paramremappingFunction - the function to recompute a value if present@returns the new value associated with the specified key, or null if no value is associated with the key*/defmerge(key: K, value: V, remappingFunction: (V, V) =>V):V
Rationale
When we never remove a value, using updateWith is very cumbersome and merge gives better readable code.
Translation to Scala
For discoverability and uniformity, the name could be changed to a variant of updateWith.
We don't do nulls in Scala so we shouldn't allow that. The return type would be Option.
the java method uses a lot of magic null values which is something scala tries to avoid. how would the signature look in terms of Option?
The whole point of merge is that options are not needed. My proposal is to not allow null arguments at all.
and, since i can't remember ever having needed something like this: can you give an example where this method is useful?
In zio-kafka we need to merge offsets, something like Map[Partition, Long] where we keep the largest offset per partition. The discussion (with code) that sparkled this issue is at zio/zio-kafka#1079
Please consider adding the
merge
method tomutable.Map
. Java defines the method as follows, see java docsRationale
When we never remove a value, using
updateWith
is very cumbersome andmerge
gives better readable code.Translation to Scala
For discoverability and uniformity, the name could be changed to a variant of
updateWith
.We don't do nulls in Scala so we shouldn't allow that. The return type would be Option.
The Scala implementation could be equivalent to:
The text was updated successfully, but these errors were encountered: