Thread overview | |||||
---|---|---|---|---|---|
|
January 11, 2018 Getting a Type from TypeInfo / Getting Variant Type | ||||
---|---|---|---|---|
| ||||
I'm using std.variant.Variant to hold a value of unknown type (not a string, could be a numeric type or a container holding multiple numeric types). I'm trying to retrieve this value with .get!T but I need the type to do that... .type gives me TypeInfo, but that's not a type so I'm not sure how to get the type without checking the TypeInfo against all possible types (which would be a massive pain). |
January 11, 2018 Re: Getting a Type from TypeInfo / Getting Variant Type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chirs Forest | On Thursday, January 11, 2018 08:59:01 Chirs Forest via Digitalmars-d-learn wrote:
> I'm using std.variant.Variant to hold a value of unknown type (not a string, could be a numeric type or a container holding multiple numeric types). I'm trying to retrieve this value with .get!T but I need the type to do that... .type gives me TypeInfo, but that's not a type so I'm not sure how to get the type without checking the TypeInfo against all possible types (which would be a massive pain).
If you have a variable of the type you put in, then you can use the typeof operator on it to get the type. e.g.
int i = 42;
static assert(is(typeof(i) == int));
It's even possible to declare variables of voldemort types that way - e.g. if you want to save the result in a member variable.
But from what I can tell, you're either going to need to know the type you'r dealing with, or you're going to need a variable of the type so that you can then use typeof on it to get its type.
- Jonathan M Davis
|
January 11, 2018 Re: Getting a Type from TypeInfo / Getting Variant Type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chirs Forest | On Thursday, 11 January 2018 at 08:59:01 UTC, Chirs Forest wrote:
> I'm using std.variant.Variant to hold a value of unknown type (not a string, could be a numeric type or a container holding multiple numeric types). I'm trying to retrieve this value with .get!T but I need the type to do that... .type gives me TypeInfo, but that's not a type so I'm not sure how to get the type without checking the TypeInfo against all possible types (which would be a massive pain).
Since types are compile-time features, you can't just turn a TypeInfo into a type, sadly. However, provided you use Algebraic and not Variant, you have a limited type universe, and can iterate over the possibilities:
import std.variant;
import std.stdio;
template dispatch(alias Fn)
{
auto dispatch(A)(A arg)
if (A.AllowedTypes.length > 0)
{
static foreach (T; A.AllowedTypes)
{
if (typeid(T) == arg.type) {
return Fn(arg.get!T);
}
}
}
}
void test(int n) {
writeln("It's an int!");
}
void test(string s) {
writeln("It's a string!");
}
unittest {
Algebraic!(int, string) a = 4;
a.dispatch!test(); // It's an int!
a = "Test";
a.dispatch!test(); // It's a string!
Algebraic!(float, int[]) b = 2.3f;
a.dispatch!test(); // Will not compile, since test has no overloads for float or int[].
Variant v = 3;
v.dispatch!test; // Will not compile, since we don't know which types it can take
}
--
Simen
|
Copyright © 1999-2021 by the D Language Foundation