Bug 8432 – format should qualify enum type with its value

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-25T06:20:00Z
Last change time
2013-02-03T13:01:48Z
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-07-25T06:20:45Z
enum X { Val } void main() { string[X] hash; hash[X.Val] = "1"; writeln(hash); X[string] hash2; hash2["foo"] = X.Val; writeln(hash2); } Output: [Val:"1"] ["foo":Val] This should really be: [X.Val:"1"] ["foo":X.Val]
Comment #1 by bearophile_hugs — 2012-07-25T07:06:16Z
There is a tradeoff here, in some situations what you ask for is good because it makes the output more qualified and explicit: import std.stdio; enum FirstEnum { foo, bae } enum SecondEnum { foo, spam } void main() { auto a = FirstEnum.foo; auto b = SecondEnum.foo; writeln(a, " ", b); } It prints: foo foo While with your proposal it prints an output that allows you to see the types: FirstEnum.foo SecondEnum.foo But in some other cases it's not so good. In most real cases enums have a name longer than your "X". With DMD 2.2060beta this program: import std.stdio; enum SomeLongEnumName { foo, bar, baz, spam } void main() { SomeLongEnumName[] a; with (SomeLongEnumName) a = [foo, bar, baz, spam, foo, bar, baz, spam, foo, bar, baz, spam]; writeln(a); } Prints: [foo, bar, baz, spam, foo, bar, baz, spam, foo, bar, baz, spam] With your proposal it prints: [SomeLongEnumName.foo, SomeLongEnumName.bar, SomeLongEnumName.baz, SomeLongEnumName.spam, SomeLongEnumName.foo, SomeLongEnumName.bar, SomeLongEnumName.baz, SomeLongEnumName.spam, SomeLongEnumName.foo, SomeLongEnumName.bar, SomeLongEnumName.baz, SomeLongEnumName.spam] That in my opinion is unacceptably noisy. And when you really need the enum names it's not too much hard to add them to the textual output (despite it requires a little longer code): import std.stdio; enum FirstEnum { foo, bae } enum SecondEnum { foo, spam } void main() { auto a = FirstEnum.foo; auto b = SecondEnum.foo; writeln(typeof(a).stringof, ".", a, " ", typeof(b).stringof, ".", b); } It prints: FirstEnum.foo SecondEnum.foo An alternative idea is to print single enums qualified with their enum name and print enums in collections without their enum name. This means this program: import std.stdio; enum SomeLongEnumName { foo, bar, baz, spam } void main() { SomeLongEnumName a; SomeLongEnumName[] b; with (SomeLongEnumName) { a = bar; b = [foo, bar, baz, spam, foo, bar, baz, spam, foo, bar, baz, spam]; } writeln(a); writeln(b); } will print: SomeLongEnumName.bar [foo, bar, baz, spam, foo, bar, baz, spam, foo, bar, baz, spam] But if you want to print many single enum variables without their enum name, how do you remove it? Adding "typeof(a).stringof, "."," before them in the writeln is simpler than removing the lading enum name. Python2 print statement adds a space between items, this is usually handy, but when you don't want that damned space, you have to use other functions from the std.library. This was so bad that in Python3 they have fixed it. Generally adding is simpler than removing. So in the end I am against your proposal, and I think the current situation is the best of all the alternatives I can think of (but maybe someone is able to find a better new idea).
Comment #2 by andrej.mitrovich — 2012-07-25T09:02:56Z
If you use enum arrays with writeln() you would typically use shorter names. My issue is that this can make reading output ambiguous: enum X { foo } enum Y { foo } void main() { X x; Y y; writeln(x); writeln(y); } outputs: foo foo In C++ you don't have to use the enum tag, but in D you do, and it would make sense to output the correct type definition. Consider another case which is why I filed this: enum Type { Class, Struct, Enum } struct Class { } struct Struct { } struct Enum { } void main() { Type[string] type = ["foo" : Type.Class]; writeln(type); } outputs: ["foo":Class] This output confused me since Class is both a struct definition and a named enumerated value. I use writeln() almost strictly for debugging and I would really want to see *valid* type info, which means a string representation that could be re-inserted into code which you could practically mix in (not that I would do that but it makes sense to keep a 1:1 mapping like this).
Comment #3 by bearophile_hugs — 2012-07-25T11:54:03Z
(In reply to comment #2) > enum Type { Class, Struct, Enum } > > struct Class { } > struct Struct { } > struct Enum { } > > void main() > { > Type[string] type = ["foo" : Type.Class]; > writeln(type); > } > > outputs: > ["foo":Class] > > This output confused me since Class is both a struct definition and a named > enumerated value. A partial solution is to add the type: writeln(typeof(type).stringof, ": ", type); It prints: Type[string]: ["foo":Class] Now you see that the keys can't be the struct name.
Comment #4 by andrej.mitrovich — 2013-01-26T16:41:39Z
Other than introducing a new format specifier, I'm not sure how to proceed. Perhaps a '%q' for "qualified" would do it. Andrei if you're reading this I'd like to get an opinion so I can work on it or close the report. Thanks.
Comment #5 by andrej.mitrovich — 2013-02-03T13:01:48Z
Not too important for me, I'm closing it.