Concat enum of strings into one string

Timoses timosesu at gmail.com
Tue Aug 14 14:07:23 UTC 2018


On Tuesday, 14 August 2018 at 13:42:04 UTC, Andrey wrote:
> Hello,
> I have a enum:
>
> enum Type : string
> {
>     One = "Q1",
>     Two = "W2",
>     Three = "R3"
> }
>
> I want to concat it in compile-time:
>
> enum result = doConcat!Type();
>
> And get this result:
>
> writeln(result); // output: "Q1 W2 R3"
>
> Delimiter here is space symbol.
> How do do it?

Here's one version:

template StringEnumValues(alias Enum)
{
     import std.traits : EnumMembers;
     string[] StringEnumValues()
     {
         string[] enumValues;
      	static foreach (member; EnumMembers!Enum)
             enumValues ~= member;
         return enumValues;
     }
}

import std.string : join;
pragma(msg, StringEnumValues!Type.join(" "));


More information about the Digitalmars-d-learn mailing list