4

I work on a class for measurements, so I would like to provide a nice API to compare two measurements.

In order to work with ordering in collection I implemented Comparator. For a nice API I implemented as well comparing operators <, <=, =>, >, ==.

So my class has the following methods:

bool operator <=(SELF other) => _value <= other._value;

bool operator <(SELF other) => _value < other._value;

bool operator >(SELF other) => _value > other._value;

bool operator >=(SELF other) => _value >= other._value;

@override
bool operator ==(Object other) =>
    identical(this, other) || other is UnitValue && runtimeType == other.runtimeType && _value == other._value;

@override
int get hashCode => _value.hashCode;

int compareTo(SELF other) => _value.compareTo(other._value);

It feels like I had to add way too much boilerplate code. Does Dart provide any mixing for getting all that implementation based on a subset of operators?

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56

1 Answers1

8

I don't think so... but you can use a simple mixin for implementing operators based on the Comparable implementation:

mixin Compare<T> on Comparable<T> {
  bool operator <=(T other) => this.compareTo(other) <= 0;

  bool operator >=(T other) => this.compareTo(other) >= 0;

  bool operator <(T other) => this.compareTo(other) < 0;

  bool operator >(T other) => this.compareTo(other) > 0;

  bool operator ==(other) => other is T && this.compareTo(other) == 0;
}

Example usage:

class Vec with Comparable<Vec>, Compare<Vec> {
  final double x;
  final double y;

  Vec(this.x, this.y);

  @override
  int compareTo(Vec other) =>
      (x.abs() + y.abs()).compareTo(other.x.abs() + other.y.abs());
}

main() {
  print(Vec(1, 1) > Vec(0, 0));
  print(Vec(1, 0) > Vec(0, 0));
  print(Vec(0, 0) == Vec(0, 0));
  print(Vec(1, 1) <= Vec(2, 0));
}
Renato
  • 12,940
  • 3
  • 54
  • 85