Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum of strings not printing expected value

Tags:

d

dmd

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

like image 710
glampert Avatar asked Mar 19 '23 00:03

glampert


1 Answers

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
like image 136
Vladimir Panteleev Avatar answered Apr 26 '23 22:04

Vladimir Panteleev