【发布时间】:2019-08-13 07:41:06
【问题描述】:
我在做一个测量类,所以我想提供一个很好的 API 来比较两个测量值。
为了处理集合中的排序,我实现了Comparator。对于一个不错的 API,我还实现了比较运算符 <、<=、=>、>、==。
所以我的班级有以下方法:
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);
感觉我不得不添加太多样板代码。 Dart 是否提供任何混合以基于运算符子集获得所有实现?
【问题讨论】:
标签: dart