Is it possible to overload the index[]
syntax for a collection/iterable type? for example I am writing a SortedList<T>
class that wraps around a standard List<T>
. and I'd just like to pass on the usage from my sorted list straight down to the underlying list object. I thought I'd read about this on the dart language tour but I can't find it now.
Add a comment
|
2 Answers
Dart editor seems pretty happy with:
operator [](int i) => list[i]; // get
operator []=(int i, int value) => _list[i] = value; // set
Try it in DartPad
Yes you can override operators. Like this:
T operator [](int index) => items[index];
But you can't overload anything in dart. Because Dart does not support operator or function overloading. That means your class can't have two function with same name but different parameters.