Giter VIP home page Giter VIP logo

Comments (4)

astralstriker avatar astralstriker commented on May 24, 2024

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

from super_enum.

xsahil03x avatar xsahil03x commented on May 24, 2024

shouldEquate maybe? @passsy any suggestions.

from super_enum.

passsy avatar passsy commented on May 24, 2024

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).

from super_enum.

xsahil03x avatar xsahil03x commented on May 24, 2024

@passsy yes, makes sense to me.

from super_enum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.