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

All objects should be Hashable #3369

Closed
DartBot opened this issue Jun 3, 2012 · 10 comments
Closed

All objects should be Hashable #3369

DartBot opened this issue Jun 3, 2012 · 10 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug
Milestone

Comments

@DartBot
Copy link

DartBot commented Jun 3, 2012

This issue was originally filed by renggl...@gmail.com


What steps will reproduce the problem?

Commonly the following rule is known regarding the implementation of == and hashChode:

  • Whenever a == b, then a.hashCode() must be same as b.hashCode().
  • If you override one, then you should override the other.

This is kind of tricky to achieve in Dart (please correct me if I am wrong), because Object implements only ==. To make it possible to put an object into a Set/Map based on its identity it needs to implement Hashable. I figured out the following code:

class A implements Hashable {

  // kind of complicated code to make an object hashable by its identity
  static int _nextHashCode = 0;
  final int _hashCode;
  A() : _hashCode = _nextHashCode++;
  int hashCode() => _hashCode;

  // already implemented in superclass, duplicated for consistency
  operator == (other) => this === other;

}

What is the expected output? What do you see instead?

  1. Object introduces an imbalance between == and hashCode.
  2. There is no easy way to get an identityHash of an object.

What version of the product are you using? On what operating system?

Dart SDK version 7904

@madsager
Copy link
Contributor

madsager commented Jun 4, 2012

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Jun 6, 2012

Recategorizing as a request for a universal hash function.


Set owner to @gbracha.
Removed Type-Defect label.
Added Type-Enhancement, Accepted labels.
Changed the title to: "All objects should be Hashable".

@gbracha
Copy link
Contributor

gbracha commented Jun 15, 2012

We expect to add a hashCode method to Object.


Added this to the M1 milestone.

@lrhn
Copy link
Member

lrhn commented Aug 9, 2012

When we do this, can we also add
  int identityHash(Object object);
as a top-level function, to match the toplevel
  bool identical(Object a, Object b);
and to make access to the identity hash of an object available even to objects that have overwritten Object's hashCode() implementation?

@gbracha
Copy link
Contributor

gbracha commented Aug 9, 2012

Your in charge of libraries :-) I don't see why not. But other's mileage may vary.

@DartBot
Copy link
Author

DartBot commented Aug 9, 2012

This comment was originally written by @seaneagan


Regarding the identical and identityHash top-level methods, these already exist as instance methods on Object (== and hashCode respectively), there is just no way to get at them. It might be nice to have a syntax to extract such instance methods from classes, whose "this" parameter would be passed as the first parameter. A syntax for this might be:

ClassName#methodName

Also, it may already possible to extract instance methods from classes via mirrors ? (but you do lose type information doing it that way).

@sethladd
Copy link
Contributor

Issue #3218 has been merged into this issue.


cc @dgrove.
cc @kwalrath.
cc @kasperl.
cc @rakudrama.

@sethladd
Copy link
Contributor

Issue #167 has been merged into this issue.

@sethladd
Copy link
Contributor

VM issue #5267
dart2js issue #5268
library issue #5266

@gbracha
Copy link
Contributor

gbracha commented Sep 21, 2012

Added Done label.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Sep 21, 2012
@DartBot DartBot added this to the M1 milestone Sep 21, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
copybara-service bot pushed a commit that referenced this issue Apr 5, 2022
Changes:
```
> git log --format="%C(auto) %h %s" a3a102a..a949b32
 https://dart.googlesource.com/pub.git/+/a949b329 Fix handling of 'git@github.com:dart-lang/pub.git' style urls in git source (#3381)
 https://dart.googlesource.com/pub.git/+/c6cd3758 Always use forward slash for relative paths in lockfile (#3379)
 https://dart.googlesource.com/pub.git/+/7d48f902 Remove unused function (#3370)
 https://dart.googlesource.com/pub.git/+/b1373d4c Fix import prefix (#3369)

```

Diff: https://dart.googlesource.com/pub.git/+/a3a102a549388a6dbfecc9252fabb618f9a2f5f7~..a949b329b1b51f5f3973a790e0a0a45897d837de/

A bunch of bugfixes.

Change-Id: I1f791cad27194359091f489f36dcc3ad2b78bb31
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240052
Reviewed-by: Jonas Jensen <jonasfj@google.com>
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
copybara-service bot pushed a commit that referenced this issue Apr 25, 2022
Changes:
```
> git log --format="%C(auto) %h %s" a3a102a..a949b32
 https://dart.googlesource.com/pub.git/+/a949b329 Fix handling of 'git@github.com:dart-lang/pub.git' style urls in git source (#3381)
 https://dart.googlesource.com/pub.git/+/c6cd3758 Always use forward slash for relative paths in lockfile (#3379)
 https://dart.googlesource.com/pub.git/+/7d48f902 Remove unused function (#3370)
 https://dart.googlesource.com/pub.git/+/b1373d4c Fix import prefix (#3369)

```

Diff: https://dart.googlesource.com/pub.git/+/a3a102a549388a6dbfecc9252fabb618f9a2f5f7~..a949b329b1b51f5f3973a790e0a0a45897d837de/

A bunch of bugfixes.

Change-Id: I1f791cad27194359091f489f36dcc3ad2b78bb31
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240052
Reviewed-by: Jonas Jensen <jonasfj@google.com>
Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants