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

Add hashCode to Map so as to be able to add it to a Set #1302

Closed
DartBot opened this issue Jan 23, 2012 · 5 comments
Closed

Add hashCode to Map so as to be able to add it to a Set #1302

DartBot opened this issue Jan 23, 2012 · 5 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report

Comments

@DartBot
Copy link

DartBot commented Jan 23, 2012

This issue was originally filed by [email protected]


What steps will reproduce the problem?

  1. Create a Set
  2. Add Map to the Set
  3. Run the script

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#

@whesse
Copy link
Contributor

whesse commented Jan 24, 2012

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.

@DartBot
Copy link
Author

DartBot commented Jan 26, 2012

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.

@DartBot
Copy link
Author

DartBot commented Jan 26, 2012

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)

@DartBot
Copy link
Author

DartBot commented Jun 14, 2012

This comment was originally written by @tomyeh


IMO, it is better to make all object hashable as Java does. Please vote up Issue #3218 if you agree. Thanks.

@floitschG
Copy link
Contributor

Every object now implements hashcode.
There might still be use-cases for Maps that compute their hash depending on its content, but that would be another issue (and I wouldn't be surprised if there was already one for that).
Closing as duplicate of issue #3218.


Added Duplicate label.
Marked as being merged into #3218.

@DartBot DartBot added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report labels Dec 5, 2012
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-duplicate Closed in favor of an existing report
Projects
None yet
Development

No branches or pull requests

3 participants