Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dart, overloading [] operator?

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.

like image 556
Daniel Robinson Avatar asked Oct 28 '13 19:10

Daniel Robinson


People also ask

Can [] operator be overloaded?

where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .

What does overloading the [] operator do?

The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++ arrays.

Does Dart support overloading?

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.


1 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

like image 147
Daniel Robinson Avatar answered Sep 22 '22 06:09

Daniel Robinson