Thread overview
Get name of enum val at compile-time?
Jan 15, 2012
Nick Sabalausky
Jan 15, 2012
Jonathan M Davis
Jan 15, 2012
Nick Sabalausky
Jan 15, 2012
Timon Gehr
Jan 15, 2012
Nick Sabalausky
Jan 15, 2012
Timon Gehr
Jan 15, 2012
Philippe Sigaud
Jan 15, 2012
Peter Alexander
January 15, 2012
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.


January 15, 2012
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)

- Jonathan M Davis
January 15, 2012
"Jonathan M Davis" <jmdavisProg@gmx.com> wrote in message news:mailman.388.1326617938.16222.digitalmars-d-learn@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



January 15, 2012
On 01/15/2012 09:34 PM, Nick Sabalausky wrote:
> import std.conv;
> enum Foo { hello }
> enum x = to!string();
>

enum x = to!string(Foo.hello);
January 15, 2012
"Timon Gehr" <timon.gehr@gmx.ch> wrote in message news:jevefv$2je6$1@digitalmars.com...
> On 01/15/2012 09:34 PM, Nick Sabalausky wrote:
>> import std.conv;
>> enum Foo { hello }
>> enum x = to!string();
>>
>
> enum x = to!string(Foo.hello);

Goddamnnit, what the fuck is wrong with me? Yes that works :)


January 15, 2012
On 01/15/2012 10:02 PM, Nick Sabalausky wrote:
> "Timon Gehr"<timon.gehr@gmx.ch>  wrote in message
> news:jevefv$2je6$1@digitalmars.com...
>> On 01/15/2012 09:34 PM, Nick Sabalausky wrote:
>>> import std.conv;
>>> enum Foo { hello }
>>> enum x = to!string();
>>>
>>
>> enum x = to!string(Foo.hello);
>
> Goddamnnit, what the fuck is wrong with me? Yes that works :)
>
>

I suspect a better error message would have prevented this.
DMD still has some potential of improvement in that area. =)
January 15, 2012
On Sun, Jan 15, 2012 at 22:19, Timon Gehr <timon.gehr@gmx.ch> wrote:

Nick:
>> Goddamnnit, what the fuck is wrong with me? Yes that works :)
>
> I suspect a better error message would have prevented this. DMD still has some potential of improvement in that area. =)

In that case, to!(Origin, Target) could be extended to deal with
to!(Origin, <empty there>).
It's been a long time since I last looked at std.conv.to, but maybe:

template to(T)
{
    T to(A...)(A args)
    {
        static if (A.length)
            return toImpl!T(args);
        else
            static assert(false, "Trying to use to!("~T.stringof~")
with no argument. What were you trying to do?");
    }
}
January 15, 2012
On 15/01/12 10:29 PM, Philippe Sigaud wrote:
> On Sun, Jan 15, 2012 at 22:19, Timon Gehr<timon.gehr@gmx.ch>  wrote:
>
> Nick:
>>> Goddamnnit, what the fuck is wrong with me? Yes that works :)
>>
>> I suspect a better error message would have prevented this.
>> DMD still has some potential of improvement in that area. =)
>
> In that case, to!(Origin, Target) could be extended to deal with
> to!(Origin,<empty there>).
> It's been a long time since I last looked at std.conv.to, but maybe:
>
> template to(T)
> {
>      T to(A...)(A args)
>      {
>          static if (A.length)
>              return toImpl!T(args);
>          else
>              static assert(false, "Trying to use to!("~T.stringof~")
> with no argument. What were you trying to do?");
>      }
> }

The compiler should be able to give you a better error message. It shouldn't be the responsibility of the programmer to provide sensible error messages when you call a function with the wrong number of arguments.