3 ms·
> even python you can do that with __eq__ i think In Python __hash__() needs to be implemented in order to put something in a set. You cannot override that in
by Gare 3y ago
> even python you can do that with __eq__ i think
In Python __hash__() needs to be implemented in order to put something in a set.
You cannot override that in Javascript, but you can roll your own set with custom hash function by wrapping a Map.
- leosanchez 3y ago> You cannot override that in Javascript True. But you they can use a "known Symbol" to implement this feature.
- Gare 3y agoImplementing this on mutable objects is a dangerous game. That would be quite the footgun (and JS has plenty of them already). IMHO it's better to be explicit in such a case. It's not complicated to implement your own set-like class that does exactly what you want.
- leosanchez 3y ago> That would be quite the footgun Could you explain why that would be a footgun? JavaScript already has known Symbols like hasInstance which is kind of similar to this.
- Gare 3y agoIt's very important to ensure that the value of hash for a given entity doesn't change if it is to be relied upon, otherwise things like Map or Set will not function properly. Therefore, one should be very careful in implementing it yourself and ideally one should use immutable hashable structures provided by the language/stdlib (like tuples or sets in Python). Until Javascript has such structures which should be enough for majority of use cases (there is record and tuple language proposal, but it's in limbo) I think it's unwise to provide a way to sloppily emulate it in user code.