Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Conversation

szeiger
Copy link
Contributor

@szeiger szeiger commented Oct 25, 2017

There is no shared abstraction for mutable and immutable Map types
because of differences in variance. This is the same as in the old
collections library.

@szeiger szeiger force-pushed the wip/map-with-default branch from 41f553d to 3ef4478 Compare October 25, 2017 16:00
Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should merge this PR (after my comments are addressed) as it provides the same feature as the one that is in the current collections.

However I’m wondering if we could take advantage of the collections redesign to improve this operation: have something that returns a CC[K, V1] instead of a Map and, most importantly, keep the default value even when transformer methods are called. I think this is doable, see how I’ve done here: https://github.com/scala/collection-strawman/pull/262/files#diff-d767a48bf7524bed5c62886aa0a3e082R53

with MapOps[K, V, Map, Map[K, V]] {

/** The same map with a given default function.
* Note: `get`, `contains`, `iterator`, `keys`, etc are not affected by `withDefault`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of giving an incomplete list of methods followed by “etc” I would clearly say what’s the impact: it provides an implementation for the default method, which is (internally) used only by the apply method.

def result(): Map[K, V] = new WithDefault[K, V](ub.result(), d)
def add(elem: (K, V)): this.type = { ub.add(elem); this }
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it should be possible to use mapFactory.newBuilder[K, V]().mapResult(WithDefault[K, V](_, d)) instead.

override def updated[V1 >: V](key: K, value: V1): WithDefault[K, V1] = new WithDefault[K, V1](underlying.updated[V1](key, value), d)
override def remove (key: K): WithDefault[K, V] = new WithDefault(underlying - key, d)
override def withDefault[V1 >: V](d: K => V1): immutable.Map[K, V1] = new WithDefault[K, V1](underlying, d)
override def withDefaultValue[V1 >: V](d: V1): immutable.Map[K, V1] = new WithDefault[K, V1](underlying, x => d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to override withDefault and withDefaultValue? The implementation is the same as the base implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not. It avoids layering several WithDefault on top of each other.

@julienrf
Copy link
Contributor

Could you also update the README to indicate that the withDefault and withDefaultValue operations are now supported?

@szeiger
Copy link
Contributor Author

szeiger commented Oct 26, 2017

You can't and shouldn't return a CC. You don't want to reimplement a WithDefault subclass for every concrete Map implementation. It wouldn't provide any advantage, either. WithDefault is more like a view (which we do not have for Maps in the general case), not a transformation. There is no rebuilding going on that could use the standard mechanisms for that. Every single affected operation needs to be reimplemented for every single subtype.

There is no shared abstraction for mutable and immutable Map types
because of differences in variance. This is the same as in the old
collections library.
@szeiger szeiger force-pushed the wip/map-with-default branch from 3ef4478 to 65bdac8 Compare October 26, 2017 17:04
@szeiger
Copy link
Contributor Author

szeiger commented Oct 26, 2017

Updated and rebased.

@julienrf julienrf merged commit d9a830a into master Oct 26, 2017
@julienrf julienrf deleted the wip/map-with-default branch October 26, 2017 19:09
@julienrf julienrf added this to the 0.6.0 milestone Oct 26, 2017
@nafg
Copy link

nafg commented Nov 5, 2017

I wonder if it should really be defined on PartialFunction...

@julienrf
Copy link
Contributor

julienrf commented Nov 5, 2017

What behavior would you recommend?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants