New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cannot extend built-in list implementation #2600
Comments
This comment was originally written by @sethladd Brought up on Stack Overflow: http://stackoverflow.com/questions/14441620/custom-collection-in-dart |
It's interesting that the workaround discussed in that question uses noSuchMethod, which has bad code size implications on JS, and probably performance overhead too (on VM or JS). Added Library-Core label. |
An option: extend Iterable<E> and implement List<E> You only have to implement |
@kevmoo Yup. IIRC that's what we're currently doing in web_ui (see lib/observe/) Still has a lot of extra methods copied though, and the overhead issues mentioned above. |
This comment was originally written by @simonpai @kevmoo, I think extending from Collection will save a few more methods for us. I strongly agree with John. It's kind of silly, in my opinion, to hide such a common implementation so that every project which needs to hack just a few things on the List needs to reinvent the wheel in its code base. This feels like you are doing some woodwork and realize there is a chainsaw right behind a thin door, but you have to use your handsaw instead just because the door is locked for some unexplained reason. |
I hope we can have a ListBase class you can extend to get full List functionality by implementing just [], []=, length and length=. Added Accepted label. |
that would be lovely! |
ListBase and ListMixin are now available in dart:collection. Added Fixed label. |
There are various places where we'd like to implement List<E>. It'd be nice to be able to extend the default growable implementation, for cases like an ObservableList where we just want to do some change tracking. This works great in other languages (e.g. Java and C#), but in Dart it has a few issues:
* the class has different names in the VM vs compiled to JS (See http://dartbug.com/949)
* Even if that was fixed GrowableObjectArray can't be subclassed in the VM.
Otherwise, what we end up doing is forwarding all of the methods:
class ObservableList<E> implements List<E> {
<...>
Iterator<E> iterator() => _items.iterator();
void forEach(void f(E element)) => _items.forEach(f);
Collection map(f(E element)) => _items.map(f);
<...>
void operator []=(int index, E value) {
_items[index] = value;
notifyChanged(...);
}
void set length(int newLength) {
_items.length = newLength;
notifyChanged(...);
}
void add(E value) {
_items.add(value);
notifyChanged(...);
}
<...>
This has unnecessary overhead--memory for the wrapper and extra generated JS code.
The text was updated successfully, but these errors were encountered: