www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get value of enum without casting

reply "tcak" <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
[code]
const enum FieldLength: uint{
	Title = 64
}

const string SQL1 = "title CHAR(" ~ std.conv.to!string( 
FieldLength.Title ) ~ ")";

void main() {
	writeln( FieldLength.Title );
	writeln( SQL1 );
}
[/code]


Result is
---------------
Title
Char(Title)


I can do cast(uint)( FieldLength.Title ) to fix this, but then I 
am repeating the type of `enum`.

Is there any way to get the type of enum without interacting with 
its items?
Is there any way to get string representation of an item of enum 
without casting?
Jul 09 2015
parent Justin Whear <justin economicmodeling.com> writes:
On Thu, 09 Jul 2015 16:20:56 +0000, tcak wrote:

 Is there any way to get the type of enum without interacting with its
 items?
std.traits.OriginalType
 Is there any way to get string representation of an item of enum without
 casting?
I think casting to the OriginalType and then using to!string is the correct way to do this.
Jul 09 2015