jaguar_serializer icon indicating copy to clipboard operation
jaguar_serializer copied to clipboard

== and hashcode generation

Open jaumard opened this issue 5 years ago • 7 comments

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

jaumard avatar Aug 28 '18 15:08 jaumard

How do we handle hashCode?

tejainece avatar Aug 28 '18 15:08 tejainece

We need to override hashCode and == operator We can use quiver/core to override easily the hashCode

On Tue, Aug 28, 2018, 17:42 Ravi Teja Gudapati [email protected] wrote:

How do we handle hashCode?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Jaguar-dart/jaguar_serializer/issues/154#issuecomment-416635670, or mute the thread https://github.com/notifications/unsubscribe-auth/AFLtgLqDQAlVZbTQMCOIKh2KXM2wieC3ks5uVWTUgaJpZM4WP0ID .

Kleak avatar Aug 28 '18 15:08 Kleak

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^^

jaumard avatar Aug 28 '18 16:08 jaumard

Ok. But how do we compute hashCode?

tejainece avatar Aug 28 '18 21:08 tejainece

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 ^

jaumard avatar Aug 29 '18 06:08 jaumard

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?

tejainece avatar Aug 29 '18 07:08 tejainece

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 ?

jaumard avatar Aug 29 '18 07:08 jaumard