Get name of enum val at compile-time?

Nick Sabalausky a at a.a
Sun Jan 15 12:34:48 PST 2012


"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message 
news:mailman.388.1326617938.16222.digitalmars-d-learn at puremagic.com...
> On Sunday, January 15, 2012 03:53:09 Nick Sabalausky wrote:
>> Is there a way to get the name of an enum value at compile-time?
>>
>> For instance:
>>
>> import std.stdio;
>> enum Foo { hello }
>> void main()
>> {
>>     writeln(Foo.hello);
>> }
>>
>> That prints "hello". But what I need is to get "hello" into a string at
>> compile-time.
>>
>> Of course, I could just manually write a ctfe-able "fooToString()", or
>> implement something like std.typecons.defineEnum (which appears to be
>> deprecated now). But I'm wondering if I'm overlooking a better solution.
>
> to!string(Foo.hello)
>

Nope. That was the first thing I tried.

With 2.057:

>type testCTEnumToString.d
import std.conv;
enum Foo { hello }
enum x = to!string();

>dmd testCTEnumToString.d -c
phobos\std\conv.d(237): Error: template std.conv.toImpl(T,S) if 
(isImplicitlyConvertible!(S,T)) does not match any function template 
declaration
phobos\std\conv.d(237): Error: template std.conv.toImpl(T,S) if 
(isImplicitlyConvertible!(S,T)) cannot deduce template function from 
argument types !(string)()
phobos\std\conv.d(237): Error: template instance toImpl!(string) errors 
instantiating template
testCTEnumToString.d(3):        called from here: to()

Doesn't work at runtime, either:

>type testRTEnumToString.d
import std.conv;
enum Foo { hello }
void main()
{
        auto x = to!string();
}

>dmd testRTEnumToString.d -c
phobos\std\conv.d(237): Error: template std.conv.toImpl(T,S) if 
(isImplicitlyConvertible!(S,T)) does not match any function template 
declaration
phobos\std\conv.d(237): Error: template std.conv.toImpl(T,S) if 
(isImplicitlyConvertible!(S,T)) cannot deduce template function from 
argument types !(string)()
phobos\std\conv.d(237): Error: template instance toImpl!(string) errors 
instantiating template
testRTEnumToString.d(5): Error: template instance std.conv.to!(string).to!() 
error instantiating





More information about the Digitalmars-d-learn mailing list