Dart 2.0: Getting type of generic parameter (typeof() analog)

4,658 views
Skip to first unread message

Vitali Liashchuk

unread,
Apr 11, 2018, 11:22:17 AM4/11/18
to Dart Misc
Hi guys,

         Could anybody clarify is there any option to get exact Type from generic parameter? 
Something like that:

T myFunc<T>()
{
    Type type = typeof(T);
}

I've tried to get T.runtimeType but this property contains only obfuscated js type... 
In c# you can achieve this functionality with typeof()

Thanks)

Erik Ernst

unread,
Apr 11, 2018, 11:46:48 AM4/11/18
to Dart Misc
On Wed, Apr 11, 2018 at 5:22 PM Vitali Liashchuk <vitali.l...@gmail.com> wrote:
Hi guys,

         Could anybody clarify is there any option to get exact Type from generic parameter? 
Something like that:

T myFunc<T>()
{
    Type type = typeof(T);
}

You can do `Type myFunc<T>() => T`, which will return the `Type` object corresponding to the exact value of the actual type argument for the given invocation.

Note that this works in Dart 2, but unless your tools are very fresh you may have a version (say, of the vm `dart`) where function type arguments are not reified, in which case `T` will just evaluate to `dynamic`. You may try to use `--preview-dart-2` in order to enable the relevant features, if they are available but not active by default.

I've tried to get T.runtimeType but this property contains only obfuscated js type... 

`T.runtimeType` is the type of the type, that is: `Type`, except that you may get an instance of a platform dependent subtype thereof. It's rather unlikely that this is what you intend to use, a plain `T` is more likely.
 
In c# you can achieve this functionality with typeof()

In Dart you directly get the run time representation of a type by evaluating the type as an expression: The C# example `System.Type type = typeof(int);` would be a simple `Type type = int;` in Dart.

However, `Type` in Dart is not intended to be an entry point to a full reflective system, `Type` values can basically be used to recognize specific types (using `==`) and printing them (maybe for debugging), and the latter is bad style in anything that goes into production.

So you can do this:

Map<Type, Function> functions = <Type, Function>{
  int: ((int x) => x + 1),
  String: ((String x) => x + " one"),
};

foo(v) {
  for (Type type in functions.keys) {
    if (v.runtimeType == type) return functions[type](v);
  }
  throw "Unknown type, cannot process";
}

main() {
  print(foo(40)); // '41'.
  print(foo("Forty")); // 'Forty one'.
}

but there are lots of limitations in an approach like that: You cannot handle instances of subtypes (so if you're using this to handle something of type `num` then `42.runtimeType` will produce `int` and there is no operation on `Type` which will tell you that `int` is a subtype of `num`), etc.
 

--
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+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/52647731-1553-42e1-b918-d3c4bf440442%40dartlang.org.


--
Erik Ernst  -  Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984

Bob Nystrom

unread,
Apr 16, 2018, 12:39:15 PM4/16/18
to General Dart Discussion
On Wed, Apr 11, 2018 at 1:01 AM, Vitali Liashchuk <vitali.l...@gmail.com> wrote:
Hi guys,

         Could anybody clarify is there any option to get exact Type from generic parameter? 
Something like that:

T myFunc<T>()
{
    Type type = typeof(T);
}

You can just use T:

T myFunc<T>()
{
    Type type = T;
}

What are you trying to do with the resulting Type object?

– bob

Reply all
Reply to author
Forward
0 new messages