Dart: Delegate the comparison operator of a derived class to comparison operator of base class

Matthias247

I want to overload the the comparison operator (==) in Dart to compare structures. Now I'm not sure how to do this for derived classes when I already have overloaded the comparison operator of the base class and want to reuse that.

Assuming I have a a base class like:

class Base
{
  int _a;
  String _b;

  bool operator ==(Base other)
  {
    if (identical(other, this)) return true;
    if (_a != other._a) return false;
    if (_b != other._b) return false;
    return true;
  }
}

Then I declare I derived class that adds additional fields and also want to overload operator==. I only want to compare the additional fields in the derived class and delegate the comparison of Base fields to the Base class. In other programming languages I could do something like Base::operator==(other) or super.equals(other), but in Dart I can't figure out what's the best way to do it.

class Derived extends Base
{
  int _c; // additional field

  bool operator ==(Derived other)
  {
    if (identical(other, this)) return true;        
    if (_c != other._c) return false; // Comparison of new field

    // The following approach gives the compiler error:
    // Equality expression cannot be operand of another equality expression.
    if (!(super.==(other))) return false;

    // The following produces "Unnecessary cast" warnings
    // It also only recursively calls the Derived operator
    if ((this as Base) != (other as Base)) return false;    

    return true;
  }
}

I guess what I could do is:

  • Compare all fields of base class also in the derived class: Is very error prone if base class get's changed and also doesn't work when base and derived are in different packages.
  • Declare an equals function with the same logic as currently operator == in it, call super.equals() to compare the base class and delegate all calls of operator== to the equals function. However it doesn't look too appealing to implement equals and operator ==.

So what's the best or recommended solution for this problem?

Matthias247

Ok, after some further experiments I figured it out on my own. It's simply calling:

super==(other)

I tried it with super.operator==(other) and super.==(other) before, but didn't expect that the simple super==(other) is sufficient.

For the given example above the correct operator is:

bool operator ==(Derived other)
  {
    if (identical(other, this)) return true;
    if (_c != other._c) return false;
    if (!(super==(other))) return false;
    return true;
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Assignment operator not available in derived class

From Dev

C++: template class with comparison operator overloading

From Dev

Can operator<< in derived class call another operator<< in base class in c++?

From Dev

How to call an operator from a base class within the derived class?

From Dev

Why is virtual operator== called on the Base class instead of the Derived class?

From Dev

Have == operator to ignore selected class members during object comparison

From Dev

Subquery in `where` with comparison operator

From Dev

JSP comparison operator behaviour

From Dev

Overloading the comparison operator== of derived class to scale for any number of base classes

From Dev

Vertica comparison operator with timestamp

From Dev

Boolean output with comparison operator

From Dev

Comparison operator return 0 ==

From Dev

Array of pointers to the base class that contains objects of derived classes and overloaded << operator

From Dev

SFINAE to enable cast operator only from derived to base class

From Dev

How to handle comparison operator for Generic Java class accepting String or Integer

From Dev

C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

From Dev

Primitive and Object comparison with == operator

From Dev

'Reversed' comparison operator in Python

From Dev

order of evaluation of comparison operator?

From Dev

Comparison operator in PySpark (not equal/ !=)

From Dev

Comparison operator in for statement

From Dev

How to overload operator << for derived classes using a shared base class?

From Dev

Assignment operator and copy constructor for class containing base class pointer to derived templated class

From Dev

operator=(double) hidden in derived class

From Dev

Array of pointers to the base class that contains objects of derived classes and overloaded << operator

From Dev

Overloading new operator in the derived class

From Dev

How to call derived assignment operator from base class?

From Dev

Non member comparison operator for a template class

From Dev

Copy assignment operator for derived class

Related Related

  1. 1

    Assignment operator not available in derived class

  2. 2

    C++: template class with comparison operator overloading

  3. 3

    Can operator<< in derived class call another operator<< in base class in c++?

  4. 4

    How to call an operator from a base class within the derived class?

  5. 5

    Why is virtual operator== called on the Base class instead of the Derived class?

  6. 6

    Have == operator to ignore selected class members during object comparison

  7. 7

    Subquery in `where` with comparison operator

  8. 8

    JSP comparison operator behaviour

  9. 9

    Overloading the comparison operator== of derived class to scale for any number of base classes

  10. 10

    Vertica comparison operator with timestamp

  11. 11

    Boolean output with comparison operator

  12. 12

    Comparison operator return 0 ==

  13. 13

    Array of pointers to the base class that contains objects of derived classes and overloaded << operator

  14. 14

    SFINAE to enable cast operator only from derived to base class

  15. 15

    How to handle comparison operator for Generic Java class accepting String or Integer

  16. 16

    C++ Base class pointer pointing to derived class object doesn't call derived equal operator function

  17. 17

    Primitive and Object comparison with == operator

  18. 18

    'Reversed' comparison operator in Python

  19. 19

    order of evaluation of comparison operator?

  20. 20

    Comparison operator in PySpark (not equal/ !=)

  21. 21

    Comparison operator in for statement

  22. 22

    How to overload operator << for derived classes using a shared base class?

  23. 23

    Assignment operator and copy constructor for class containing base class pointer to derived templated class

  24. 24

    operator=(double) hidden in derived class

  25. 25

    Array of pointers to the base class that contains objects of derived classes and overloaded << operator

  26. 26

    Overloading new operator in the derived class

  27. 27

    How to call derived assignment operator from base class?

  28. 28

    Non member comparison operator for a template class

  29. 29

    Copy assignment operator for derived class

HotTag

Archive