| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
January 25, 2014 Generating an enum from a tuple of Types? | ||||
|---|---|---|---|---|
| ||||
Is it possible to generate a enum from a tuple of types without string mixins?
------------
struct S(Types...)
{
enum Tag
{
//?
}
}
------------
where the tag enum should have Types.length members. The exact names of the enum members don't matter and could be numbered, for example: Tag._1, Tag._2, ...
This is what I have right now, using string mixins: http://dpaste.dzfl.pl/536e0be9
It implements a simple C-like tagged union in a generic way, so stuff like this is possible:
-------------------------------------------
alias Value = TaggedUnion!(int, string);
auto val = Value("Hello");
auto vals = val.get!string();
val.set(0);
final switch(val.tag)
{
case Value.tagType!int:
break;
case Value.tagType!string:
break;
}
-------------------------------------------
It's basically std.variant Algebraic with less features, but it should be much faster as it doesn't use TypeInfo.
| ||||
January 25, 2014 Re: Generating an enum from a tuple of Types? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | On Saturday, 25 January 2014 at 15:38:39 UTC, Johannes Pfau wrote: > Is it possible to generate a enum from a tuple of types without string > mixins? > ------------ > struct S(Types...) > { > enum Tag > { > //? > } > } > ------------ Without mixins altogether... dunno. But nothing stops you from making a eponymous template :) http://dpaste.dzfl.pl/0af9dd7e | |||
January 25, 2014 Re: Generating an enum from a tuple of Types? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | If one wants to generate code with new identifiers, usage of string mixins is pretty much unavoidable. | |||
January 25, 2014 Re: Generating an enum from a tuple of Types? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Johannes Pfau | On 01/25/14 16:38, Johannes Pfau wrote: > Is it possible to generate a enum from a tuple of types without string mixins? > ------------ > struct S(Types...) > { > enum Tag > { > //? > } > } > ------------ > > where the tag enum should have Types.length members. The exact names of the enum members don't matter and could be numbered, for example: Tag._1, Tag._2, ... Well, if you don't need names then just use the index directly. Eg, see 'DiscUnion.type' in http://forum.dlang.org/thread/mtkzbwfgwsstndxbesgy@forum.dlang.org#post-mailman.555.1377703234.1719.digitalmars-d-learn:40puremagic.com (A newer compiler may allow for that offset-of calculation at CT) artur | |||
January 25, 2014 Re: Generating an enum from a tuple of Types? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | Am Sat, 25 Jan 2014 18:55:54 +0100
schrieb Artur Skawina <art.08.09@gmail.com>:
>
> Well, if you don't need names then just use the index directly.
>
> Eg, see 'DiscUnion.type' in
>
> http://forum.dlang.org/thread/mtkzbwfgwsstndxbesgy@forum.dlang.org#post-mailman.555.1377703234.1719.digitalmars-d-learn:40puremagic.com
>
> (A newer compiler may allow for that offset-of calculation at CT)
>
> artur
Thanks for all the answers, I guess I'll keep the string mixin.
I don't really need the names but I need the enum to support final
switch.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply