Why does this snippet print the enum identifiers instead of their values?

Kenji Hara k.hara.pg at gmail.com
Tue May 28 09:06:07 PDT 2013


On Tuesday, 28 May 2013 at 11:49:25 UTC, Gary Willoughby wrote:
> Why does the following snippet print:
>
> "Started name revision" instead of "Started my-app 1.0a"?
>
> import std.stdio;
>
> enum application : string
> {
> 	name     = "my-app",
> 	revision = "1.0a",
> }
>
> void main(string[] arguments)
> {
> 	writefln("Started %s %s", application.name, 
> application.revision);
> }

This is not compiler bug, it is an intended behavior of writefln.

writefln always print the name for named enum members.

enum E1 { a, b }
writefln("%s", E1.a);    // prints a

enum E2 { a = 10, b = 20 }
writefln("%s", E2.a);    // prints a

enum E3 : string { a = "aaa", b = "bbb" }
writefln("%s", Ee.a);    // prints a

If you want to make a set of compile time values, you can write 
as follows:

struct application  // or class
{
     enum name     = "my-app",
          revision = "1.0a";
}

Kenji Hara


More information about the Digitalmars-d-learn mailing list