How to create a List<X> if X is not available at compile time -- (WAS: Does this makes sense (as in Java): <T extends AClass> List<T> find( Class<T> clazz ) ?)

24 views
Skip to first unread message

Iván Zaera Avellón

unread,
Apr 17, 2013, 3:17:02 AM4/17/13
to mi...@dartlang.org
Hi John:

Thanks for the reply. 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.

What I would like to return is a real List<Entry> so that anyone can check what it contains and also to make checked mode fail if someone passes a List<OtherThing> to a function receiving a List<Entry>. If the find() method returns a List, I can pass that list to a function receiving a List<OtherThing> and the check wouldn't fail.

So, I suppose that my real question is: how can we create a List<X> where X is some class that we don't have available at compile time? Is this possible with mirrors? If not, IMHO, some method should be provided to do this. It would be really useful for library/frameworks developers to returned typed collections.

Cheers,
Ivan


El martes, 16 de abril de 2013, John Messerly escribió:
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
 
 

Ladislav Thon

unread,
Apr 17, 2013, 3:28:13 AM4/17/13
to mi...@dartlang.org
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

Iván Zaera Avellón

unread,
Apr 17, 2013, 3:41:13 AM4/17/13
to misc
I suppose we will have to live with the fact that generics "kinda-suck". I mean, I like templates in C++, generics in Java and Dart because they make code more readable but every implementation I see of this feature is always half-broken or counterintuitive.

I suppose this is another case of something that would be great but, in the end, cannot be done in a reasonable way.

I formally give up here ;-): it doesn't make sense to return a typed list in third party libraries if the core language has such things in methods map, expand and fold. Also I prefer an untyped list (which is not so bad as even if it is not checked it will somehow fail if objects are of unexpected type) to the java syntax. Imagine passing a ClassMirror to Iterable.map() just to be able to return a typed list!!!




2013/4/17 Ladislav Thon <lad...@gmail.com>

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

--

Ladislav Thon

unread,
Apr 17, 2013, 3:52:42 AM4/17/13
to mi...@dartlang.org
I formally give up here ;-)

Oh, there's one more thing you can do: star http://dartbug.com/254. No results guaranteed, though :-)

LT 

Iván Zaera Avellón

unread,
Apr 17, 2013, 4:38:35 AM4/17/13
to misc
Thx :-)


2013/4/17 Ladislav Thon <lad...@gmail.com>

I formally give up here ;-)

Oh, there's one more thing you can do: star http://dartbug.com/254. No results guaranteed, though :-)

LT 

--
Reply all
Reply to author
Forward
0 new messages