Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

DataField property to ignore field from equatable props #20

Closed
xsahil03x opened this issue Dec 9, 2019 · 4 comments
Closed

DataField property to ignore field from equatable props #20

xsahil03x opened this issue Dec 9, 2019 · 4 comments

Comments

@xsahil03x
Copy link
Owner

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.

@xsahil03x xsahil03x changed the title DataField property to ignore field from equatable. DataField property to ignore field from equatable props Dec 9, 2019
@astralstriker
Copy link
Collaborator

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

@xsahil03x
Copy link
Owner Author

shouldEquate maybe? @passsy any suggestions.

@passsy
Copy link
Collaborator

passsy commented Dec 15, 2019

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
Copy link
Owner Author

@passsy yes, makes sense to me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants