digitalmars.D.learn - "name" of enum members
- spir (21/21) Feb 11 2011 Hello,
- Jesse Phillips (2/7) Feb 11 2011 Maybe to!string(Enum.i) should return the enum name with it's field. Thi...
- spir (7/14) Feb 11 2011 That's what I wrote, precisely (see example in OP), and it returns only ...
- Jesse Phillips (2/14) Feb 11 2011 It look like you were asking how you could do it, not that to!string sho...
- bearophile (10/13) Feb 11 2011 Is this good enough?
- spir (16/27) Feb 11 2011 More generally, I think default %s and to!string should systematically o...
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
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
On 02/11/2011 06:49 PM, Jesse Phillips wrote:spir Wrote:That's what I wrote, precisely (see example in OP), and it returns only "i". denis -- _________________ vita es estrany spir.wikidot.comHello, 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
spir Wrote:On 02/11/2011 06:49 PM, Jesse Phillips wrote: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.spir Wrote:That's what I wrote, precisely (see example in OP), and it returns only "i".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
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
On 02/11/2011 07:39 PM, bearophile wrote:spir:Yo! thank you Bearophile.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)); }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.comA bit strange that '%s' does not produce the same string as to!string...I agree, I have a bug report on this.
Feb 11 2011