tc39 / proposal-setmap-offrom

https://tc39.es/proposal-setmap-offrom/
39 stars 9 forks source link

Collect use cases #25

Open bakkot opened 4 years ago

bakkot commented 4 years ago

I'd like to have some specific use cases to motivate this proposal.

ljharb commented 4 years ago

In the case of the weak collections, it’s highly impractical/difficult to construct one from others, due to being unable to iterate them - and it’s impossible to do so in a robust prototype-method-call way.

In the case of the normal collections, it’s a convenience method much like Array.from - currently you’d have to do something like new Map([...map1, ...map2, ...map3]) or new Map(maps.reduce((prev, map) => [...prev, ...map], [])), both of which are awkward, have perf implications, and are easy to screw up.

bakkot commented 4 years ago

it’s highly impractical/difficult to construct one from others

Are you assuming that WeakMap.from(weakmap) would give you a copy of the map? That would make sense, but is not how they are currently specified.

currently you’d have to do something like new Map([...map1, ...map2, ...map3])

I'm not sure how Map.from would help with that.

ljharb commented 4 years ago

Yes, i do assume it wouldn’t be !==, just like the Array static’s.

Map.from([map1, map2, map3])? I would expect Map.from to accept an iterable of Map-likes - ie, an iterable of iterables of entries, not a single Map (same for the weak ones)

bakkot commented 4 years ago

Yes, i do assume it wouldn’t be !==, just like the Array static’s.

That wasn't the question. As specified, all .from methods take an iterable, which WeakMap is not.

I would expect Map.from to accept an iterable of Map-likes

... Why? I assumed it would take an iterable of entries, just as Array.from takes an iterable of values.

ljharb commented 4 years ago

hm, those are both good points. I suppose arrays are first-class enough that that doesn’t present any difficulties.

These methods seem much less useful to me without the ability to create a WeakMap from two WeakMaps. The strong methods are just sugar, so without the weak ones i don’t see much value in them existing.

zloirock commented 4 years ago

It seems to me that this is a good start for this concept.

bakkot commented 4 years ago

I think it is potentially reasonable to extend WeakMap.from to be able to clone WeakMaps. Opened #26 to track.