Discussion:
[dart-misc] Dart 2.0: Getting type of generic parameter (typeof() analog)
Vitali Liashchuk
2018-04-11 08:01:58 UTC
Permalink
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()
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/typeof

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/52647731-1553-42e1-b918-d3c4bf440442%40dartlang.org.
'Erik Ernst' via Dart Misc
2018-04-11 15:46:33 UTC
Permalink
Post by Vitali Liashchuk
Hi guys,
Could anybody clarify is there any option to get exact Type from
generic parameter?
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
Post by Vitali Liashchuk
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.
Post by Vitali Liashchuk
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.
Post by Vitali Liashchuk
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/typeof
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
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
<https://groups.google.com/a/dartlang.org/d/msgid/misc/52647731-1553-42e1-b918-d3c4bf440442%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
Erik Ernst - Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 KÞbenhavn K, Denmark
CVR no. 28866984
--
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/CACDCfDSAfZSipc0kRkN0UNQjKik7mTKmbXZXkseqGUfZDjUqhQ%40mail.gmail.com.
'Bob Nystrom' via Dart Misc
2018-04-16 16:38:40 UTC
Permalink
On Wed, Apr 11, 2018 at 1:01 AM, Vitali Liashchuk <
Post by Vitali Liashchuk
Hi guys,
Could anybody clarify is there any option to get exact Type from
generic parameter?
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
--
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/CAF8T8Lw3sSHyOzmHuzhTUjDoQGqAA9s%3DhEj0haQ1imSUtt2v7A%40mail.gmail.com.
Loading...