Dart doesn't have generic methods. If you would've used generic methods, usually you just replace that type with "dynamic". For example:List<T> find<T>(String query) {var result = <T>[];...}becomes:List find(String query) {var result = [];...}The way the type system works, List<dynamic> can be assigned to List<Entry> without causing an error in checked mode.
--On Tue, Apr 16, 2013 at 12:41 AM, Iván Zaera Avellón <iza...@gmail.com> wrote:
Hi list:--I know generics are different in Java and Dart (Dart generics are not erased at runtime) so I understand the construction in this mail subject could be written differently in Dart, but I just wanted to illustrate what I try to achieve.Say we have an ORM library with a find() method and a base class for persistent objects. If we had an entity called Entry, we would write:class Entry extends PersistentObject {...}And then, to find something we would have a method like:List<PersistentObject> find( String query ) {...}Now, the problem is that, even if find() creates Entry objects inside, it cannot return a List<Entry> because Entry is a class in the application domain, thus the library cannot declare it in its interface. I mean it should return List<Entry> and, in fact, the returned List is full of Entry objects, but its type is List<PersistentObject>.If I try to use the returned List for calling a method like:printEntries( List<Entry> entries )It fails in checked mode because "List<PersistentObject> != List<Entry>". This can be fixed by returning an untyped List in find, or by changing printEntries signature, but in both cases we lose all benefits of type checking.So here's my question: how can this be done? Is there any construct currently in the language (maybe using mirrors)? If none, does it makes sense to do something similar to Java's expression "<T extends PersistentObject> List<T> find( Class<T> clazz )"? Or just "List<? extends PersistentObject> find()"?Any ideas?Regards,Ivan
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
I understand that you can return a plain List and it is accepted by List<Entry> methods, but that doesn't really solve the problem that the List is still untyped.
I understand that you can return a plain List and it is accepted by List<Entry> methods, but that doesn't really solve the problem that the List is still untyped.Given that core libraries developers resigned on that and the Iterable.map method just returns Iterable<dynamic> (the same situation appears with expand and fold), I sugest you just give up :-) And I'm half-serious about that. Maybe more than a half.LT
--
I formally give up here ;-)
I formally give up here ;-)Oh, there's one more thing you can do: star http://dartbug.com/254. No results guaranteed, though :-)LT
--