-
Notifications
You must be signed in to change notification settings - Fork 20
Iterating on the design #20
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
Comments
That sounds workable to me (altho i'd want the valueFn to be in an options bag, so more "internal hooks" could be added in the future). |
Maybe we could simply overload |
An options bag sounds good. |
I think the function being internal state to the collection might cause some usability issues as people have existing code that does the following: let value = map.get(value) || generateDefault();
// mutate / replace value
map.set(key, value); Having function init(k) {
return map.get(k); // should synchronously introduce it if it doesn't exist
}
function result(k) {
return map.get(k); // should create an error/tdz
} Other possible things are stuff like inserting synchronous values if the value would be known at the time of the Additionally, one of the big usages of mine is to update existing values. With Record/Tuple/primitives/frozen values/etc. this does lean towards needing an update function. I think having a defaulted value is useful, but I do not think it would be the best path at this time unless we can convince ourselves that these state-less/read-only values are not needing to be updated in the common case. In my experience, if you get a default value you often want to perform some kind of update operation on it afterwards anyway. |
I've moved feedback from several issues to a revised design in #21 |
Don't forget about counters (or any other updates with immutable values, where you can't just map.set(k, 1 + (map.has(k) ? map.get(k) : 0))
map.set(k, map.has(k) ? map.get(k) + 1 : 1)) This use case is not covered by default values. |
The new design direction is focusing on the get/insert if missing usecase, with update being left for a future proposal. |
We reviewed this proposal back when it was presented, but I didn't manage to get our notes up.
One of the major comments was that the api is unintuitive as it tries to do too many things. There might be a better way to achieve the desired effect. A number of folks mentioned defaultdict as being the thing that they miss when writing js. The most common usage is the following:
if (!map.has(k)) { m.set(k, []) }; m.get(k).push(val);
A suggestion from @jorendorff was
new Map ( [iterable [, valueFn]] )
. When the user callsmap.get(key)
with no second argument, and no entry for key exists, and valueFn was specified, it’ll call valueFn(key) and insert and return the resulting value. Same for WeakMap.The text was updated successfully, but these errors were encountered: