www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - print enum value rather name from enum X : string

reply Marc <jckj33 gmail.com> writes:
If I have an enum like this:

 enum S  : string {
	foo = "a",
	baa = "b"
}
when I printed it, to my surprise I get the enum field name rather value:
 writefln("%s v%s", S.foo, S.baa);	
output:
 foo vbaa
instead of
 a vb
a cast solves it but without cast everywhere I need that enum member, is there any other way to do it? otherwise I'll have to switch to a class with static immutable strings, right?
Feb 12 2018
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, February 12, 2018 17:07:50 Marc via Digitalmars-d-learn wrote:
 If I have an enum like this:
 enum S  : string {

 foo = "a",
 baa = "b"

}
when I printed it, to my surprise I get the enum field name rather value:
 writefln("%s v%s", S.foo, S.baa);
output:
 foo vbaa
instead of
 a vb
a cast solves it but without cast everywhere I need that enum member, is there any other way to do it? otherwise I'll have to switch to a class with static immutable strings, right?
If you actually use the enum values anywhere other than with anything from std.conv, std.format, or std.stdio, then when they get converted to strings, you get their actual values. It's just that those modules specifically grab the names of the enum members when converting enums to strings, since in all cases other than with strings, it's generally desirable that when converting an enum member to string, you get the name - and with enums with a base type of string, whether you want the name or the value depends on what you're trying to do. Both can be useful. So, when dealing with std.format, std.conv, and std.stdio, if you want an enum of base type string to be treated as a string, then you'll have to force it with a cast. Anywhere else, if they get converted to string, then you'll get their values. If that is unacceptable for your use case for whatever reason, then you'll have to try a different solution. What solution would then work best would presumably depend on whatever it is you're actually trying to do. - Jonathan M Davis
Feb 12 2018
parent reply Marc <jckj33 gmail.com> writes:
On Monday, 12 February 2018 at 17:29:47 UTC, Jonathan M Davis 
wrote:
 On Monday, February 12, 2018 17:07:50 Marc via 
 Digitalmars-d-learn wrote:
 [...]
If you actually use the enum values anywhere other than with anything from std.conv, std.format, or std.stdio, then when they get converted to strings, you get their actual values. It's just that those modules specifically grab the names of the enum members when converting enums to strings, since in all cases other than with strings, it's generally desirable that when converting an enum member to string, you get the name - and with enums with a base type of string, whether you want the name or the value depends on what you're trying to do. Both can be useful. So, when dealing with std.format, std.conv, and std.stdio, if you want an enum of base type string to be treated as a string, then you'll have to force it with a cast. Anywhere else, if they get converted to string, then you'll get their values. If that is unacceptable for your use case for whatever reason, then you'll have to try a different solution. What solution would then work best would presumably depend on whatever it is you're actually trying to do. - Jonathan M Davis
Thanks for you always well-thought-out answer. I was going to print it with writefln() calls more than anywhere else so to avoid casts in all those places, which would make it ugly, I just used
 enum foo = "a";
 enum baa = "b";
which I found to be more common D-idiomatic.
Feb 12 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, February 13, 2018 01:55:59 Marc via Digitalmars-d-learn wrote:
 Thanks for you always well-thought-out answer. I was going to
 print it with writefln() calls more than anywhere else so to
 avoid casts in all those places, which would make it ugly, I just
 used

 enum foo = "a";
 enum baa = "b";
which I found to be more common D-idiomatic.
Well, those are generally referred to as manifest constants rather than enums (though the spec calls them anonymous enums), and they're fundamentally different from enums in the sense that they aren't grouped together and don't create a type. If all you want is a bunch of constants, and you're not planning on doing something like having a function take a variable of an enum type, then it generally makes more sense to use manifest constants than enums, but if you want to actually treat the enum as a type, then you need an actual enum. So, really, it comes down to what you're trying to do. - Jonathan M Davis
Feb 12 2018