xsahil03x / super_enum

Create super-powered dart enums similar to sealed classes in Kotlin
https://pub.dev/packages/super_enum
MIT License
116 stars 13 forks source link

DataField property to ignore field from equatable props #20

Closed xsahil03x closed 4 years ago

xsahil03x commented 4 years ago

Currently, we don't have any option or property to mark a DataField to be ignored from equatable props. We should add a property for the same.

astralstriker commented 4 years ago

suggest a name for the said property, I am struggling to find a good name :P

xsahil03x commented 4 years ago

shouldEquate maybe? @passsy any suggestions.

passsy commented 4 years ago

Instead of blindly adding this feature I'd like to see a real-world example where this is useful.

  1. Immutable sealed classes should definitely not have a custom equals method. Every member counts.
  2. Mutable classes should not have an equals method at all. See Avoid defining custom equality for mutable classes

And I've never seen a good example where a partially equals method implementation actually made sense. It might only be used for a special use case and might be better implemented using a custom comparison method, not the operator ==().

void main() {
  final itemA = Item("A");
  final itemB = Item("B")..added = false;
  final itemB2 = Item("B")..added = true;
  print(itemA == itemB); // false
  print(itemB == itemB2); // false
  print(itemA.customEquals(itemB)); // false
  print(itemB.customEquals(itemB2)); // true
}

class Item {
  Item(this.name);
  final String name;

  // mutable property
  bool added;
}

extension ItemEquality on Item {
  /// Only checks for the name 
  bool customEquals(dynamic other) => (other is Item) && name == other.name;
}

My recommendation: Add a property to skip generating the equals method at all. It could be useful for mutable super_enums. Users of such classes then could implement an extension for that type to implement their own equals method. (Sadly overriding operator ==() doesn't work with extensions).

xsahil03x commented 4 years ago

@passsy yes, makes sense to me.