Enum of types

evilrat evilrat666 at gmail.com
Mon May 20 23:09:41 PDT 2013


On Tuesday, 21 May 2013 at 04:58:35 UTC, Diggory wrote:
> On Tuesday, 21 May 2013 at 04:36:57 UTC, Ali Çehreli wrote:
>> Sounds like std.variant.Algebraic:
>>
>>  http://dlang.org/phobos/std_variant.html#.Algebraic
>>
>> Ali
>
> I don't see how I could use that. Algebraic is for storing a 
> value, I'm trying to store just a type.

you can't just get and store type as value. well, maybe you can 
but i'm unaware of how to achieve this. the closest thing is 
enum/aa mix, it should be useful unless you need this type stuff 
with ctfe. i also hope you know what are you doing, since this is 
never was safe way, but it is maybe required for scripting 
languages or so. anyway here is the closest thing i've used.

module fail;

import std.variant;
import std.stdio;

enum IDs
{
FIRST,
SECOND,
THIRD,
}

struct A {}

private enum keyFIRST = 1;
private enum keySECOND = "string";
private enum keyTHIRD = A();

Variant[IDs] Types;

static this()
{
  Types = [
		IDs.FIRST: Variant(keyFIRST),
		IDs.SECOND: Variant(keySECOND),
		IDs.THIRD: Variant(keyTHIRD)
		];
}

void main()
{
writeln(Types);

assert( Types[IDs.FIRST].type is typeid(int) );
assert( Types[IDs.SECOND].type is typeid(string) );
assert( Types[IDs.THIRD].type is typeid(A) );
}


More information about the Digitalmars-d-learn mailing list