飞镖,重载[]运算符?

Dan*_*son 16 operator-overloading dart

是否可以重载[]集合/可迭代类型的索引语法?例如,我正在编写一个SortedList<T>包含标准的类List<T>.我只想将我的排序列表中的用法直接传递给基础列表对象.我以为我会在飞镖语游中读到这个,但我现在找不到了.

Dan*_*son 22

飞镖编辑似乎很满意:

operator [](int i) => list[i]; // get
operator []=(int i, int value) => _list[i] = value; // set
Run Code Online (Sandbox Code Playgroud)

DartPad中尝试一下


小智 6

是的,您可以覆盖运算符。像这样:

T operator [](int index) => items[index];
Run Code Online (Sandbox Code Playgroud)

但你不能在 Dart 中重载任何东西。因为 Dart 不支持运算符或函数重载。这意味着您的类不能有两个名称相同但参数不同的函数。