On 4/6/14, 6:49 PM, Walter Bright wrote:
On 4/6/2014 4:17 PM, Andrei Alexandrescu wrote:
On 4/6/14, 10:52 AM, Walter Bright wrote:
I use enums a lot in D. I find they work very satisfactorily. The way
they work was deliberately designed, not a historical accident.

Sorry, I think they ought to have been better. -- Andrei

Sorry, yer wrong!

This program compiles and flag free and no cast in sight but fails at runtime. Textbook example of unsound type design.

import std.stdio;

enum A { x = 2, y = 4 }

void main()
    A a = A.x | A.y;
    final switch (a)
        case A.x: break;
        case A.y: break;

The "|" operator converts back to an A. It shouldn't. In this case it provides a value not only outside the enum range, but even greater than A.max (when converted to integer).

I'm fine with "yes, it's unsound, but we wanted to do flags and we couldn't find a better solution", but this "it's deliberate and it's good" I just find difficult to get behind.


Reply via email to