www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "name" of enum members

reply spir <denis.spir gmail.com> writes:
Hello,

To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a way 
to get back this "name"?
Here are my best trials:

unittest {
     enum TC : uint {i=1};
     writefln("%s   %s   %s", TC.i, TC.i.stringof, to!string(TC.i));
}
==>
1   cast(TC)1u   i

A bit strange that '%s' does not produce the same string as to!string... 
Anyway, the only solution seems to be parsing the result of stringof to get 
what's inside (), then compose it with the result of to!string: a bit too much 
of a burden, what do you think?
Hints welcome :-)

Thank you for reading,
Denis
-- 
_________________
vita es estrany
spir.wikidot.com
Feb 11 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
spir Wrote:

 Hello,
 
 To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a
way 
 to get back this "name"?
 Here are my best trials:

Maybe to!string(Enum.i) should return the enum name with it's field. This makes since because it is trying to return its name, and for named enums, where it comes from is important.
Feb 11 2011
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
spir Wrote:

 On 02/11/2011 06:49 PM, Jesse Phillips wrote:
 spir Wrote:

 Hello,

 To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a way
 to get back this "name"?
 Here are my best trials:

Maybe to!string(Enum.i) should return the enum name with it's field. This makes since because it is trying to return its name, and for named enums, where it comes from is important.

That's what I wrote, precisely (see example in OP), and it returns only "i".

It look like you were asking how you could do it, not that to!string should do it for you. My thought is added it to bugzilla.
Feb 11 2011
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
On 02/11/2011 06:49 PM, Jesse Phillips wrote:
 spir Wrote:

 Hello,

 To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a way
 to get back this "name"?
 Here are my best trials:

Maybe to!string(Enum.i) should return the enum name with it's field. This makes since because it is trying to return its name, and for named enums, where it comes from is important.

That's what I wrote, precisely (see example in OP), and it returns only "i". denis -- _________________ vita es estrany spir.wikidot.com
Feb 11 2011
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
spir:

 To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a
way 
 to get back this "name"?

Is this good enough? import std.stdio, std.conv; enum TC { A, B, C } void main() { writeln(typeof(TC.A).stringof, ".", to!string(TC.A)); }
 A bit strange that '%s' does not produce the same string as to!string...

I agree, I have a bug report on this. Bye, bearophile
Feb 11 2011
prev sibling parent spir <denis.spir gmail.com> writes:
On 02/11/2011 07:39 PM, bearophile wrote:
 spir:

 To denote a member 'm' of an enum 'e', one needs to write "e.m". Is there a way
 to get back this "name"?

Is this good enough? import std.stdio, std.conv; enum TC { A, B, C } void main() { writeln(typeof(TC.A).stringof, ".", to!string(TC.A)); }

Yo! thank you Bearophile.
 A bit strange that '%s' does not produce the same string as to!string...

I agree, I have a bug report on this.

More generally, I think default %s and to!string should systematically output the literal notation able to reconstruct the string'ed element. I mean, as far as possible without getting into much complication (in cases of struct or class object, which often have data members not read in by the contructor). About the annoying case of strings, is there a tool func somewhere to reconstruct an escaped version? (like eg when the string holds '"') I have looked for it without success. Else, i guess Phobos very much needs that. (the equivalent of python's %r or repr() for strings) Simple, clear, consistent, informative, useful. Denis -- _________________ vita es estrany spir.wikidot.com
Feb 11 2011