acutmore / proposal-keyby

1 stars 1 forks source link

Set vs Hashmap #7

Open bencoveney opened 9 months ago

bencoveney commented 9 months ago

Excuse me sticking my nose in 😄 I was having a read through and wondered whether this would feel right as an extension to Set. For example, in the README currently:

There is no capability to override this behavior and allow two different objects to be treated equal within the collection.

This sounds at-odds to me with the basic description of a set.

The Set object lets you store unique values of any type...

_From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set_

The keyBy callback is effectively a hashing function, making Set behave like a Hashmap. For that reason, I would think alternative to this would be to add a Hashmap or Hashset collection instead, into which people could pass JSON.stringify or whatever hashing behaviour they would like.

To me this would feel like a better conceptual fit. If I received a Hashmap from some calling code, I think I would be aware of the fact that Hashmaps make less guarantees around uniquess and could proceed with caution. If I receive a Set on the other hand, I may be less likely to question the uniqueness check and may be bitten by keyBy being configured in a way I didn't expect. Another way to say it would be that Set becomes less "trustworthy".

The are some equivalents to this stuff in C#: Hashset and Hashtable. I bring them up as points of comparison, because:

ljharb commented 9 months ago

The point is to change the definition of "unique". Set's existing definition doesn't actually match the language's already - -0 and -0 are treated the same.

acutmore commented 1 week ago

A significant part of me agrees that HashMap or HashSet might be good names for this; it's what other languages have. But I keep feeling like it's not quite right because: