Is it possible to extend a generic list with my my own specific list. Something like:
class Tweets<Tweet> extends List<T>
And how would a constructor look like, if I wanted to construct with my own constructor:
Datasource datasource = new Datasource('http://search.twitter.com/search.json');
Tweets tweets = new Tweets<Tweet>(datasource);
And how to call the parent constructor then, as this is not done in a extended class?
This is what i found out to extend list behavior:
See adapted Tweet example bellow. It uses custom Tweets method and standard list method.
Note that add/addAll has been removed.
Output:
[hello, world, hello]
[hello, hello]
[hello, hello]
Code:
import 'dart:collection';
class Tweet {
String message;
Tweet(this.message);
String toString() => message;
}
class Tweets<Tweet> extends ListBase<Tweet> {
List<Tweet> _list;
Tweets() : _list = new List();
void set length(int l) {
this._list.length=l;
}
int get length => _list.length;
Tweet operator [](int index) => _list[index];
void operator []=(int index, Tweet value) {
_list[index]=value;
}
Iterable<Tweet> myFilter(text) => _list.where( (Tweet e) => e.message.contains(text));
}
main() {
var t = new Tweet('hello');
var t2 = new Tweet('world');
var tl = new Tweets();
tl.addAll([t, t2]);
tl.add(t);
print(tl);
print(tl.myFilter('hello').toList());
print(tl.where( (Tweet e) => e.message.contains('hello')).toList());
}
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