Thread overview
Why does this snippet print the enum identifiers instead of their values?
May 28, 2013
Gary Willoughby
May 28, 2013
bearophile
May 28, 2013
John Colvin
May 28, 2013
John Colvin
May 28, 2013
Jesse Phillips
May 28, 2013
bearophile
May 28, 2013
Kenji Hara
May 28, 2013
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);
}
May 28, 2013
Gary Willoughby:

> 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);
> }

It looks like a compiler bug :-) (Enums of strings are not well supported. enums were invented mostly for integral types.) If it's not already in Bugzilla then I suggest to add it there.

By the way, in D enum names as "application" usually start with an upper case ("Application"), and the name of the main array is usually "args". Sticking with common practices is better, unless you have (uncommon) real reasons to do something different.

Bye,
bearophile
May 28, 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);
> }

I don't really know what the correct way of doing this is. There really ought to be a property .value of enums.

The stringof property will return what you want, although annoyingly with quotation marks around it ([1 .. $-1] on the end will clear that up of course). For types other than string it returns cast(application)value as a string, which is not particularly helpful.

BTW this only happens with named enums, anonymous enums print the value not the name.


I don't know how much any of this is intended behaviour and how much is just by chance.
May 28, 2013
On Tuesday, 28 May 2013 at 12:16:51 UTC, John Colvin wrote:
> There really ought to be a property .value of enums.

or alternatively .name of course. Either way there should be a way to choose whether you want the name or the value.
May 28, 2013
I do not believe this is a bug. You should be able to print the value by casting to string. (to!() will not work as it will provide the same results)

Usually enumeration values are not descriptive for how they should be used. And when printing to the screen it is usually for debugging or some other task where a meaningful name would be better.

String enum's are a little different since there value can be fairly descriptive. But still they usually are not descriptive about how they should be used. And it would be odd for enum of int to print the enum name while string enum to print the value.
May 28, 2013
On Tuesday, 28 May 2013 at 14:35:22 UTC, Jesse Phillips wrote:
> I do not believe this is a bug.

You are right, I was wrong. writeln is meant to write the name of enums and not their contents.

Bye,
bearophile
May 28, 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