www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Enum of types

reply "Diggory" <diggsey googlemail.com> writes:
I want to create an enum of some number of types, eg. { ubyte, 
ushort, uint, float }

It should be straightforward to get a member of the enum from the 
type itself. Basically I have an object that stores some typed 
data, but the type may not be known at compile time, only the set 
of possible types. When constructing the object the user should 
be able to tell it what type of data it will hold. Also the 
underlying type of the enum should be efficient to store and 
compare.

So far the best I can come up with is using a mixin and CTFE to 
generate the code for an enum plus a template function to convert 
from a type to a member of the enum, but it seems like there 
might be a better way?
May 20 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/20/2013 08:20 PM, Diggory wrote:
 I want to create an enum of some number of types, eg. { ubyte, ushort,
 uint, float }

 It should be straightforward to get a member of the enum from the type
 itself. Basically I have an object that stores some typed data, but the
 type may not be known at compile time, only the set of possible types.
 When constructing the object the user should be able to tell it what
 type of data it will hold. Also the underlying type of the enum should
 be efficient to store and compare.

 So far the best I can come up with is using a mixin and CTFE to generate
 the code for an enum plus a template function to convert from a type to
 a member of the enum, but it seems like there might be a better way?
Sounds like std.variant.Algebraic: http://dlang.org/phobos/std_variant.html#.Algebraic Ali
May 20 2013
parent reply "Diggory" <diggsey googlemail.com> writes:
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.
May 20 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/20/2013 09:58 PM, 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.
But you did say you needed to store data: "... an object that stores some typed data ... the user should be able to tell it what type of data it will hold." With my current limited understanding, I still think Algebraic would be a solution. Ali
May 20 2013
parent "Diggory" <diggsey googlemail.com> writes:
On Tuesday, 21 May 2013 at 05:48:31 UTC, Ali Çehreli wrote:
 On 05/20/2013 09:58 PM, 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.
But you did say you needed to store data: "... an object that stores some typed data ... the user should be able to tell it what type of data it will hold." With my current limited understanding, I still think Algebraic would be a solution. Ali
The data is stored elsewhere in a black-box completely outside D's jurisdiction. Also the type of the data is supplied first, and the data itself is supplied at a later time. I gave up on the mixin idea but have come up with this: import std.typetuple; struct TypeEnum(T...) { private int index; private this(int index) { this.index = index; } template from(U) { static assert(staticIndexOf!(U, T) != -1, "Type is not in the list of possible types for this TypeEnum"); public enum from = TypeEnum!T(staticIndexOf!(U, T)); } } Example usage: alias TypeEnum!( ubyte, ushort, float ) gpElementType; gpElementType test = gpElementType.from!float; Might make a nice addition to std.variant, no?
May 20 2013
prev sibling parent "evilrat" <evilrat666 gmail.com> writes:
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) ); }
May 20 2013
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 21 May 2013 at 03:20:56 UTC, Diggory wrote:
 I want to create an enum of some number of types, eg. { ubyte, 
 ushort, uint, float }

 It should be straightforward to get a member of the enum from 
 the type itself. Basically I have an object that stores some 
 typed data, but the type may not be known at compile time, only 
 the set of possible types. When constructing the object the 
 user should be able to tell it what type of data it will hold. 
 Also the underlying type of the enum should be efficient to 
 store and compare.

 So far the best I can come up with is using a mixin and CTFE to 
 generate the code for an enum plus a template function to 
 convert from a type to a member of the enum, but it seems like 
 there might be a better way?
This looks like a runtime action which is unrelated to enum. Since you almost cannot have unknown type but some possible value of it, you can use typeids with pure byte arrays to store value, but you have to either use fixed size arrays (thus limiting possible cases) or dynamic arrays (which may be impossible).
May 21 2013