www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert to string an enum item

reply tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
[code]
import std.stdio;
import std.conv;

enum Values: ubyte{ One = 1, Two = 2 }

void main(){
	writeln( std.conv.to!string( Values.One ) );
}
[/code]

Output is "One".

casting works, but to be able to cast correctly, I need to tell 
compiler that it is "ubyte".

Isn't there any NON-HACKISH solution to print out the value of 
enum item? And do not decrease the performance as well please. It 
runs on web server.


Problem comes from that:
I have written a struct for JsonArray. It has multiple append 
methods. One of them is "appendNumber".

public ref JsonArray appendNumber(N)( N num )
if( __traits( compiles, {auto x=num.min + num.max;} ) || 
__traits( compiles, {auto x=num.min_normal;} ) )
{
	appendJSString( std.conv.to!string( num ) );
	return this;
}

While I am passing an enum item to this function, as it has "min" 
and "max" attributes, it is accepted. But the resulting string is 
the name of enum item instead of its value. I do not want casting 
while enum knows its type already.
Dec 23 2015
parent reply Alex Parrill <initrd.gz gmail.com> writes:
On Wednesday, 23 December 2015 at 13:11:28 UTC, tcak wrote:
 [code]
 import std.stdio;
 import std.conv;

 enum Values: ubyte{ One = 1, Two = 2 }

 void main(){
 	writeln( std.conv.to!string( Values.One ) );
 }
 [/code]

 Output is "One".

 casting works, but to be able to cast correctly, I need to tell 
 compiler that it is "ubyte".

 Isn't there any NON-HACKISH solution to print out the value of 
 enum item? And do not decrease the performance as well please. 
 It runs on web server.
So you want the enum's integer value? Try casting it to an integer. writeln(cast(ubyte) Values.One);
Dec 23 2015
next sibling parent Meta <jared771 gmail.com> writes:
On Wednesday, 23 December 2015 at 17:43:52 UTC, Alex Parrill 
wrote:
 On Wednesday, 23 December 2015 at 13:11:28 UTC, tcak wrote:
 [code]
 import std.stdio;
 import std.conv;

 enum Values: ubyte{ One = 1, Two = 2 }

 void main(){
 	writeln( std.conv.to!string( Values.One ) );
 }
 [/code]

 Output is "One".

 casting works, but to be able to cast correctly, I need to 
 tell compiler that it is "ubyte".

 Isn't there any NON-HACKISH solution to print out the value of 
 enum item? And do not decrease the performance as well please. 
 It runs on web server.
So you want the enum's integer value? Try casting it to an integer. writeln(cast(ubyte) Values.One);
He already mentioned that. What you want to do is use std.traits.
Dec 23 2015
prev sibling parent Meta <jared771 gmail.com> writes:
On Wednesday, 23 December 2015 at 17:43:52 UTC, Alex Parrill 
wrote:
 On Wednesday, 23 December 2015 at 13:11:28 UTC, tcak wrote:
 [code]
 import std.stdio;
 import std.conv;

 enum Values: ubyte{ One = 1, Two = 2 }

 void main(){
 	writeln( std.conv.to!string( Values.One ) );
 }
 [/code]

 Output is "One".

 casting works, but to be able to cast correctly, I need to 
 tell compiler that it is "ubyte".

 Isn't there any NON-HACKISH solution to print out the value of 
 enum item? And do not decrease the performance as well please. 
 It runs on web server.
So you want the enum's integer value? Try casting it to an integer. writeln(cast(ubyte) Values.One);
He already mentioned that. What you want to do is use std.traits.OriginalType (https://dlang.org/phobos/std_traits.html#OriginalType) like so: to!string(cast(OriginalType!Values)Values.one)
Dec 23 2015