36

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.

2 Answers 2

64

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

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.