mythz
2018-04-11 16:20:45 UTC
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!
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.
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.