www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String representation of enum value

reply Marek Janukowicz <marek janukowicz.net> writes:
Given following code:

enum ErrorCode {
  BAD_REQUEST,
  UNKNOWN
}

ErrorCode code;

is there any way to get string representation of code with regard to values 
defined in enum? I mean - if code == 0, I'd like to get a string 
"BAD_REQUEST", if it's == 1, I'd like to get string a "UNKNOWN". Of course 
I'd like to use something more consise and elegant than a switch :)

-- 
Marek Janukowicz
Aug 04 2013
parent reply "Robik" <szadows gmail.com> writes:
On Sunday, 4 August 2013 at 13:57:20 UTC, Marek Janukowicz wrote:
 Given following code:

 enum ErrorCode {
   BAD_REQUEST,
   UNKNOWN
 }

 ErrorCode code;

 is there any way to get string representation of code with 
 regard to values
 defined in enum? I mean - if code == 0, I'd like to get a string
 "BAD_REQUEST", if it's == 1, I'd like to get string a 
 "UNKNOWN". Of course
 I'd like to use something more consise and elegant than a 
 switch :)
I think std.conv.to!string is what are you looking for :) http://dpaste.dzfl.pl/e1c38f8f
Aug 04 2013
parent Marek Janukowicz <marek janukowicz.net> writes:
Robik wrote:
 I think std.conv.to!string is what are you looking for :)
 
 http://dpaste.dzfl.pl/e1c38f8f
Wow, so simple, thanks -- Marek Janukowicz
Aug 04 2013