Discussion:
[dart-misc] What's possible with Dart Generics at runtime in Flutter
mythz
2018-04-11 16:20:45 UTC
Permalink
I'm developing a serialization library that works in flutter so I'm
avoiding Mirrors and would like to know what's possible with runtime
generics in Dart/flutter to find out what needs to be code-gen'ed and what
can be automated with generic routines.

Firstly how can we can get the runtime time of a generic argument?

T create<T>() {
var runtimeTypeOfT = ... ?
}

Is it possible to create a new instance of T where T has an empty
constructor, e.g:

T create<T>() {
var instance = new T();
return instance;
}

This is possible in C# without reflection by using the `where T : new()`
type constraint, how can we do this in Dart/Flutter?

Can we create a generic factory function which creates a generic list with
the runtime argType?

List createList(String argType) {
var newGenericList = ... // create List<argType>?
return newGenericList;
}

So that we can could use it to create generic lists like:

List<int> intList = createList("int");
List<String> stringList = createList("String");


As a current workaround/hack I'm generating a dictionary of factory
constructors like:

{
'List<String>': () => new List<String>(),
'Map<String,int>': () => new Map<String,int>(),
}

But I would like to avoid needing to do this and would like to be able to
use a generic routine if possible.

Thanks!
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/451c52dd-a552-4062-9457-ca7dcd75aa25%40dartlang.org.
'Bob Nystrom' via Dart Misc
2018-04-16 16:42:44 UTC
Permalink
Post by mythz
I'm developing a serialization library that works in flutter so I'm
avoiding Mirrors and would like to know what's possible with runtime
generics in Dart/flutter to find out what needs to be code-gen'ed and what
can be automated with generic routines.
Firstly how can we can get the runtime time of a generic argument?
T create<T>() {
var runtimeTypeOfT = ... ?
}
"T":

T create<T>() {
var runtimeTypeOfT = *T*;
}
Post by mythz
Is it possible to create a new instance of T where T has an empty
T create<T>() {
var instance = new T();
return instance;
}
This is possible in C# without reflection by using the `where T : new()`
type constraint, how can we do this in Dart/Flutter?
No, there's no way to do this in Dart currently without using some sort of
code generation approach.
Post by mythz
Can we create a generic factory function which creates a generic list with
the runtime argType?
List createList(String argType) {
var newGenericList = ... // create List<argType>?
return newGenericList;
}
List<int> intList = createList("int");
List<String> stringList = createList("String");
No, there's no way to do this either. Supporting something like this in
general would make it *very* hard to do tree shaking, which is vital for
controlling code size on the web.
Post by mythz
As a current workaround/hack I'm generating a dictionary of factory
{
'List<String>': () => new List<String>(),
'Map<String,int>': () => new Map<String,int>(),
}
This is, I think, the best current solution. It's not ideal, but
metaprogramming is generally pretty hard in a language that is compiled
ahead of time and where users also care deeply about generated code size.
Dart 2 is more like C++ than, say, C# where you can rely on a VM and a JIT
and where generated code size isn't usually a big concern.

I hope that eventually we can add some more powerful static metaprogramming
features to Dart, but it's a really challenging problem and there are few
successful languages out there in the same space that we can learn from.

Cheers!

– bob
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8LwzE2qpOyYo7T%3D4B21y9H84EUvnbg7o0CumR%3D%2BKL2KqkQ%40mail.gmail.com.
Loading...