-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add hashCode to Map so as to be able to add it to a Set #1302
Comments
I think that hashCode should not be added to Map, for the following reasons: If the hash code depends on the keys and values, so equal maps have equal hash codes, then any mutable map can have its hash code changed at any time. If only constant maps will have hash codes, then the problem is that I don't see any const constructors for maps except for map literals, so only const map literals will be hashable. If maps will be put into hashtables, then equality needs to be overridden for maps, so that two maps will be equal if they have the same keys and values. So many additional capabilities will need to be added to maps, and it will be hard to make them have no impact on the normal usage, which doesn't use a hash code. The hash code can be computed as needed, but the overriding of equality can't depend on whether the hash has been used or not. Deciding whether to make maps immutable once they have been hashed, or to make their hash code not final, is also problematic. So it seems that all of these additional capabilities and restrictions really belong in a subclass of Map, HashableMap, that implements them all without affecting the native map class. So I would propose not making Map implement Hashable. I'm not sure about whether a HashableMap class should be added to the corelib. If so, a HashableMap class would probably have a freeze method, which makes it immutable and gives it a hash code. Added Area-Library, Triaged labels. |
This comment was originally written by [email protected] whesse: I see your point. ImmutableMap.from( { name: value } ) would be make for sensible hashcode-ing and I can still get the benefit of collecting unique instances. |
This comment was originally written by [email protected] This may be related to 472: Underspecified Set<T> and Map<K,V> semantics (http://code.google.com/p/dart/issues/detail?id=472) |
Every object now implements hashcode. |
This issue was originally filed by [email protected]
What steps will reproduce the problem?
main() {
var all = [];
all.add({"name": "foo", "type": "monkey"});
all.add({"name": "bar", "type": "cheetah"});
var filtered = new Set.from(all);
}
What is the expected output? What do you see instead?
I'd expect no error (to be able to add a Map to a set).
NoSuchMethodError
An unhandled exception has been thrown
NoSuchMethodException - receiver: 'Instance of
'Library:'dart:coreimpl' Class: LinkedHashMapImplementation'' function
name: 'hashCode' arguments: []]
0. Function: 'Object.noSuchMethod' url: 'bootstrap' line:93 col:3
1. Function: 'HashMapImplementation._probeForAdding@924b4b8' url:
'bootstrap_impl' line:3207 col:40
2. Function: 'HashMapImplementation.[]=' url: 'bootstrap_impl' line:
3315 col:32
3. Function: 'HashSetImplementation.add' url: 'bootstrap_impl' line:
3423 col:16
4. Function: 'HashSetImplementation.HashSetImplementation.from' url:
'bootstrap_impl' line:3413 col:14
5. Function: 'main' url: 'set_from.dart' line:5 col:17
Please provide any additional information below.
This functionality is available at in Java, so it should be possible to generate a uniquely identifying hashcode.
Some discussion:
https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/5a9411331e302735#
The text was updated successfully, but these errors were encountered: