Reputation: 14848
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.
Upvotes: 38
Views: 13106
Reputation: 101
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.
Upvotes: 9
Reputation: 14848
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
Upvotes: 66