Enum of types

Diggory diggsey at googlemail.com
Mon May 20 23:07:19 PDT 2013


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?


More information about the Digitalmars-d-learn mailing list