Automated D code editing?

Lubos Pintes lubos.pintes at gmail.com
Sat Oct 13 00:07:18 PDT 2012


Although I thought about refactoring, which I know is not available yet, 
this was very interesting example (for me as newbye).
Dňa 12. 10. 2012 21:43 Andrej Mitrovic  wrote / napísal(a):
> On 10/12/12, Lubos Pintes <lubos.pintes at gmail.com> wrote:
>> Hi,
>> I am still playing with DGUI library. Besides other things, I would like
>> to convert enum names
>
> You mean at compile-time? Try this: http://dpaste.dzfl.pl/06b95c3f
>
> Copied below:
>
> import std.conv;
> import std.string;
> import std.traits;
>
> private @property string toCamelCase(E)()
>      if (is(E == enum))
> {
>      string result;
>      result ~= "enum CamelCase {\n";
>      foreach (Member; EnumMembers!E)
>      {
>          foreach (word; to!string(Member).split("_"))
>          {
>              result ~= word.capitalize;
>          }
>          result ~= " = " ~ Member.stringof ~ ",\n";
>      }
>      result ~= "}";
>      return result;
> }
>
> template CamelCase(E)
>      if (is(E == enum))
> {
>      mixin(toCamelCase!E);
> }
>
> enum SOME_ENUM
> {
>      VAL_FIRST = -4,
>      VAL_SECOND
> }
>
> alias CamelCase!SOME_ENUM SomeEnum;
>
> void main()
> {
>      SomeEnum en1 = SomeEnum.ValFirst;
>      SOME_ENUM en2 = SOME_ENUM.VAL_FIRST;
>      assert(en1 == en2);
> }
>



More information about the Digitalmars-d-learn mailing list