飞镖,重载[]运算符?

时间:2013-10-28 19:43:15

标签: operator-overloading dart

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

2 个答案:

答案 0 :(得分:19)

飞镖编辑似乎非常满意:

operator [](int i) => list[i]; // get
operator []=(int i, int value) => _list[i] = value; // set

DartPad

中试用

答案 1 :(得分:2)

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

T operator [](int index) => items[index];

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

相关问题