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

== and hashcode generation #154

Open
jaumard opened this issue Aug 28, 2018 · 7 comments
Open

== and hashcode generation #154

jaumard opened this issue Aug 28, 2018 · 7 comments

Comments

@jaumard
Copy link
Contributor

jaumard commented Aug 28, 2018

Would be nice to have == and hashcode generated for us as it's annoying to make them each time we change the model

@tejainece
Copy link
Member

How do we handle hashCode?

@Kleak
Copy link
Member

Kleak commented Aug 28, 2018 via email

@jaumard
Copy link
Contributor Author

jaumard commented Aug 28, 2018

Having an annotation at model level @GenModelHelper or something to override == and hashcode, in addition a clone and toString methods would be just a feature killer for me^^

@tejainece
Copy link
Member

tejainece commented Aug 28, 2018

Ok. But how do we compute hashCode?

@jaumard
Copy link
Contributor Author

jaumard commented Aug 29, 2018

When using IntelliJ, it can generate those with some shortcut, here is what it generate:


  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is CashRegisterState &&
              runtimeType == other.runtimeType &&
              quantity == other.quantity &&
              product == other.product &&
              amountCharacters == other.amountCharacters &&
              mode == other.mode;

  @override
  int get hashCode =>
      quantity.hashCode ^
      product.hashCode ^
      amountCharacters.hashCode ^
      mode.hashCode;

So it's quite easy to generate I'll say :) just list all fields with .hashCode and join with ^

@tejainece
Copy link
Member

Shouldnt it be:

  int get hashCode =>
      quantity?.hashCode ?? 0 ^
      product?.hashCode ?? 0 ^
      amountCharacters?.hashCode ?? 0 ^
      mode?.hashCode ?? 0;

What happens when there is a an iterable or a map?

@jaumard
Copy link
Contributor Author

jaumard commented Aug 29, 2018

Hum yes looks more right, on map/iterable IntelliJ does the same, just .hashCode on them, maybe worth taking a look at how built_value is doing it ?

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

3 participants