[Issue 8432] write needs to print full enum type

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 25 07:06:19 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8432


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #1 from bearophile_hugs at eml.cc 2012-07-25 07:06:16 PDT ---
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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list