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

hashCode generator enhancement #4784

Closed
mzdm opened this issue Aug 26, 2020 · 8 comments
Closed

hashCode generator enhancement #4784

mzdm opened this issue Aug 26, 2020 · 8 comments

Comments

@mzdm
Copy link
Contributor

mzdm commented Aug 26, 2020

When the built-in generator (ALT + Insert on Windows) is used for example in model classes, we can use it for generating ==, hashCode and toString() methods.

Let's say we have this model class:

class Example {
  const Example({
    this.category,
    this.id,
    this.isFavorite,
    this.name,
    this.price,
    this.startDate,
    this.expirationDate,
  });

  final Category category;
  final int id;
  final bool isFavorite;
  final String name;
  final int price;
  final DateTime startDate;
  final DateTime expirationDate;
}

Generated hashCode method for this example is:

@override
  int get hashCode =>
      category.hashCode ^
      id.hashCode ^
      isFavorite.hashCode ^
      name.hashCode ^
      price.hashCode ^
      startDate.hashCode ^
      expirationDate.hashCode;

Instead, I think it would be better if it was generating this
using hashValues
(if it would have more than 1 argument and less than 20)

@override
  int get hashCode => hashValues(category, id, isFavorite, name, price, startDate, expirationDate);
@mzdm
Copy link
Contributor Author

mzdm commented Aug 28, 2020

Additional details:
combining hashes with XOR operation may cause issues (see felangel/equatable#39).

class Point {
  final int x = 3;
  final int y = 4;
}

and

class Point {
  final int x = 4;
  final int y = 3;
}

will hash to the same value.

hashValues method uses Jenkins hash function but to use it the import 'dart:ui' is needed.

however ...

there's now some activity hinting that a method that combines hashes is coming from ui package to main sdk core so it can be used without importing.
related: dart-lang/sdk#11617 dart-lang/sdk#25217

@pq
Copy link
Contributor

pq commented Aug 28, 2020

/fyi @scheglov @bwilkerson

@bwilkerson
Copy link

Yes, and when the new static methods in Object land, we should definitely update the generator to make use of them. Given how soon they will likely land, I don't think it's worth changing it to use hashValues.

@stevemessick
Copy link
Member

@mzdm it is actually acceptable to have two non-equal objects that hash to the same value, although always better if they don't.

Closing since the is an issue for the Dart SDK.

@bwilkerson
Copy link

Actually, I believe that the generator is implemented in IntelliJ, though it's probably in the Dart plugin. (On the Mac it's command-M.) It uses a dialog to query which fields to include in the comparisons and that isn't something that server-based tools can do.

@alexander-doroshko

@bwilkerson bwilkerson reopened this Aug 28, 2020
@alexander-doroshko
Copy link
Contributor

True, items from the Generate popup (hashCode, ==(), toString(), Override Methods, Implement Methods) are implemented in the Dart plugin.

@stevemessick
Copy link
Member

https://youtrack.jetbrains.com/issue/IDEA-249561

@lukepighetti
Copy link

How do we determine when Object.hashAll will be available?

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

6 participants