Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 7358 - final switch over enum should add throwing default in debug mode #673

Merged
merged 1 commit into from
Oct 30, 2012

Conversation

yebblies
Copy link
Member

@yebblies yebblies commented Feb 1, 2012

Add a throwing default case when compiled with asserts on.

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

@andralex
Copy link
Member

This is good. We must discuss disabling "|" for enums (which I think is terrible) separately.

@alexrp
Copy link
Member

alexrp commented Sep 25, 2012

@andralex I think disabling that would be very unreasonable; it would make using enums for sort-of-type-safe bit flags annoying.

@andralex
Copy link
Member

@alexrp ... and would make using enums for REALLY GOOD CATEGORICAL TYPES awesome.

@donc
Copy link
Collaborator

donc commented Sep 26, 2012

Possibly you could make a case for bitwise operations on enums which have had their size explicitly specified and all members given explicit values (eg, enum Foo : int { A = 1, B = 2 }, the Windows header files use enums in that way), but I don't think there's any case for allowing bitwise operations on enums which haven't explicitly specified a type.

@ghost
Copy link

ghost commented Sep 26, 2012

@andralex: and would make porting C/C++ code difficult.

@jmdavis
Copy link
Member

jmdavis commented Oct 17, 2012

| on enums is fine. Having the result be an enum is not, and that is what needs to be changed. Any and all operations which result in an enum should be guaranteed to be a valid enum value. Now, because of casting, it'll still always be possible to get an invalid enum value, so making final switch throw in that case is good, but if casting isn't involved, it should be impossible for operating on enum values to result in a value which is of the enum type and not be a valid enum value.

@ghost
Copy link

ghost commented Oct 18, 2012

@jmdavis: It would be great if we could actually define valid operations on enums.

For example if you want to only define | on enums you would define opBinary (and other operators when necessary).

enum Flag
{
    a = 0x01,
    b = 0x02;

    @disable auto opBinary(string op, T)(T rhs) { }
    auto opBinary(string op : "|", T)(T rhs) { return this | rhs; }
}

Which would be similar to:

struct Flag
{
    int flag;

    enum a = 0x01;
    enum b = 0x02;

    @disable auto opBinary(string op, T)(T rhs) { }
    auto opBinary(string op : "|", T)(T rhs) { return Flag(this.flag | rhs.flag); }
}

void main()
{
    Flag f = Flag(Flag.a) | Flag(Flag.b);
    assert(f.flag == (0x01 | 0x02));
}

braddr pushed a commit to braddr/dmd that referenced this pull request Oct 22, 2012
fix for "some std.json improvements"
…bug mode at least

Add a throwing default case when compiled with asserts on.
@yebblies
Copy link
Member Author

Rebased, should be ready.

donc pushed a commit that referenced this pull request Oct 30, 2012
Issue 7358 - final switch over enum should add throwing default in debug mode
@donc donc merged commit 638d81d into dlang:master Oct 30, 2012
@denis-sh
Copy link
Contributor

@andralex I think disabling that would be very unreasonable; it would make using enums for sort-of-type-safe bit flags annoying.

Just want to mention that at least one library solution for bit flags is ready: unstd.typecons.flagEnum.

@yebblies yebblies deleted the issue7358 branch November 22, 2013 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants