飞镖比较运算符 [英] Dart comparison operators

查看:78
本文介绍了飞镖比较运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个用于度量的类,所以我想提供一个不错的API来比较两个度量。



为了处理集合中的订购,我实现了比较器。对于一个不错的API,我还实现了比较运算符< < = = > > ==



所以我的课有以下方法:

 布尔运算符< =(SELF other)=> _value< = other._value; 

布尔运算符<(SELF other)=> _value< other._value;

布尔运算符>(SELF other)=> _value> other._value;

bool运算符> =(SELF other)=> _value> = other._value;

@override
布尔运算符==(其他对象)=>
same(this,other)||另一个是UnitValue&& runtimeType == other.runtimeType& _value == other._value;

@override
int get hashCode => _value.hashCode;

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

感觉就像我不得不添加太多样板代码。 Dart是否会为基于操作符子集的所有实现提供任何混合方式?

解决方案

我不这么认为。 ,但您可以基于 Comparable 实现使用简单的混合函数来实现运算符:

  mixin Compare< T>在可比较的< T>上{
bool运算符< =(Tother)=> this.compareTo(other)< = 0;

布尔运算符> =(Tother)=> this.compareTo(other)> = 0;

个布尔运算符<(Tother)=> this.compareTo(other)< 0;

布尔运算符>(Tother)=> this.compareTo(other)> 0;

布尔运算符==(other)=>其他是T&& this.compareTo(other)== 0;
}

示例用法:



<具有可比较< Vec> ;、比较< Vec>的pre> 类Vec。 {
最终双倍x;
最终双倍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));
}


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?

解决方案

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));
}

这篇关于飞镖比较运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆