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.
where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .
The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.
Dart did not support overloading originally because it was a much more dynamic language where the declared types did not have any semantic effect. That made it impossible to use static type based overload resolution.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With