简体   繁体   English

在Dart中将类型定义为“可比较”

[英]Defining a type to be a `Comparable` in Dart

This question might stem from an inadequate understanding of OOP in Dart, but anyway: 这个问题可能源于对Dart对OOP的理解不足,但是无论如何:

I've been working on a project for which one of my base classes extends ListBase from dart:collections . 我一直在从事一个项目,我的一个基类为此项目从dart:collections扩展了ListBase It is useful in some cases I come across to be able to get the sorted indices of these List-like objects (without actually sorting them). 在某些情况下,能够获得这些类似List的对象的排序索引很有用(实际上没有对其进行排序)。

As a parent class I have something like (heavily simplified): 作为父类,我有类似的东西(经过简化):

abstract class A<T> extends ListBase<T> {
  List<T> internalList;
  List<int> orderedIndices();

  ...

}

An example descendant is something like: 后代示例如下:

class B extends A<num> {

  ...

  @override
  List<int> orderedIndices() => new List<int>.generate(
    internalList.length,(i) => i)..sort(
      (i, j) => internalList[i].compareTo(internalList[j]));
}

For example, if an instance of B has [1, 5, 2, 4, 3] as its internalList then orderedIndices will return [0, 2, 4, 3, 1] , as expected. 例如,如果B的实例具有[1, 5, 2, 4, 3] orderedIndices [1, 5, 2, 4, 3]作为其internalListorderedIndices将按orderedIndices返回[0, 2, 4, 3, 1] orderedIndices [0, 2, 4, 3, 1]

I would like to put the code that implements orderedIndices into class A 's definition, however, because it would be identical in any of A 's descendants as long as the specified type T is Comparable (ie has compareTo defined). 但是,我想将实现orderedIndices的代码放入类A的定义中,因为只要指定的类型TComparable (即,具有compareTo定义),它在A的任何后代中都是相同的。 I don't know how do this, however, because without knowing the type T , Dart has no way of knowing whether T instances are indeed Comparable and coughs at the compareTo if I try to put the code in A 's definition. 我不知道如何做到这一点,但是,因为不知道类型T ,飞镖无法知道是否方式T情况确实是Comparable ,并在咳嗽compareTo如果我尝试将代码放在A的定义。

Although it would work, I balk at the idea of copy-and-pasting code into descendants of the same class... Is there some way I can tell Dart in my definition of A that T will be a Comparable ? 尽管可行,但我反对将代码复制粘贴到同一类的子孙中的想法...是否可以通过某种方式告诉Dart在A定义中T将是可Comparable

If you want to limit T of A to types that are comparable. 如果要将A T限制为可比较的类型。 That's done by putting a bound on T : 这是通过在T上设置T

abstract class A<T extends Comparable<T>> extends ListBase<T> {
  ...
  List<int> orderedIndices() => new List<int>.generate(
    internalList.length,(i) => i)..sort(
      (i, j) => internalList[i].compareTo(internalList[j]));
  ...

If not all version of A are comparable, then you can't do that. 如果不是所有版本的A都具有可比性,那么您将无法做到这一点。 Then you can make a helper-subclass of A which is: 然后,您可以创建A的辅助子类:

abstract class ComparableA<T extends Comparable<T>> extends A<T> {
   List<int> orderedIndices() => new List<int>.generate( ....
   ...
}

Then you can do class B extends ComparableA<num> ... to share the comparison behavior when it's possible, and classes extending A directly will need to make their own implementation of the method. 然后,您可以做class B extends ComparableA<num> ...以在可能的情况下共享比较行为,而直接扩展A类将需要自己实现该方法。

Finally, you could change your specifiation of orderedIndices to: 最后,您可以将orderedIndices更改为:

List<int> orderedIndics(int Function(T, T) compare) {
  return List.generate(internalList.length,(i) => i)..sort(
      (i, j) => compare(internalList[i], internalList[j]));
}

Then you can choose per invocation how to compare the elements of internalList . 然后,您可以选择每次调用如何比较internalList的元素。 If T is Comparable , you can use Comparable.compare as the implementation. 如果TComparable ,则可以使用Comparable.compare作为实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM