I have the following D program:
import std.stdio;
enum XYZ : string
{
A = "hello",
B = "foo",
C = "bar"
}
void main()
{
writeln(XYZ.A);
writeln(XYZ.B);
writeln(XYZ.C);
}
That prints:
A
B
C
I would expect it to print hello
, foo
, and bar
. Why is the program printing the name of the constant instead of its value? And how to print the string value then?
Compiler is DMD v2.063.2
Why is the program printing the name of the constant instead of its value?
As a general rule, writeln
and other functions print the names of enum values, since for e.g. numeric types the name conveys more information than a number.
And how to print the string value then?
Just cast it to a string:
cast(string)XYZ.A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With