Skip to content
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

Map with keys of any type #30

Closed
berdario opened this issue Jan 23, 2016 · 3 comments
Closed

Map with keys of any type #30

berdario opened this issue Jan 23, 2016 · 3 comments

Comments

@berdario
Copy link

While technically correct (you can insert them and enumerate through all the values in a Map/Set), "use any type" ignores the fact that you cannot actually query a Map for keys of any type

The most amazing part of Maps is that we are no longer limited to just using strings. We can now use any type as a key, and it will not be type-casted to a string.

let map = new Map([
    ['name', 'david'],
    [true, 'false'],
    [1, 'one'],
    [{}, 'object'],
    [function () {}, 'function']
]);

This might give people misconceptions, since it overlooks the fact that EcmaScript doesn't define equality for non-primitive values.

That is map.get({}) or map.get(function () {}) will give you back undefined (You'd have the same problem with Array, Date and any other object)

For this reason, people might want to use a library that actually shoulder that burden like:

(and, as an added bonus, the structures these libs define are immutable)

@sQu1rr
Copy link

sQu1rr commented Jan 23, 2016

as with any js object {} !== {}. Referential equality
let a = {}; // then a === a
so map.get(a) will work for map.set(a, ...)

@DrkSephy
Copy link
Owner

This is true. I should make a mention that we are no longer limited to strings, but we can only effectively use maps with primitive values. I'll go ahead and update the example to make note of this. Much appreciated! 👍

@berdario
Copy link
Author

Uh, good. Thanks for handling this so quickly. 👍

In case people might want to nitpick: I tried to be careful in my choice of words:

EcmaScript doesn't define equality for non-primitive values

As opposed to equality for non-primitives (by) reference, which is (should be?) well defined, much simpler to implement, but not what people want in most cases. It actually seems like the ES standard doesn't talk about "reference equality" and "value equality", so I don't think that people would get confused if we're not using that terminology... I just felt like I should mention it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants