January 26, 2012
> I agree, enum variable should only contain one of the enumerated values. Here's an example how current way may lead to unexpected result:
>
> enum Foo { A = 1, B }
>
> void bar( Foo foo ) {
>    final switch( foo ) {
>        case Foo.A:
>            writeln( "A" );
>            return;
>        case Foo.B:
>            writeln( "B" );
>            return;
>    }
>    writeln( "Unreachable branch" );
> }
>
> int main() {
>    bar( Foo.A | Foo.B );
>    return 0;
> }
>
> It is intuitive to assume that the final switch will always hit one of it's branches, yet this doesn't work here.

Valid example.
As I said one could introduce something like @flags but I guess a library solution is preferred.
I still wonder though if implicit conversion to the basetype has any merit.
January 26, 2012
Trass3r:

> As I said one could introduce something like @flags but I guess a
> library solution is preferred.
> I still wonder though if implicit conversion to the basetype has
> any merit.

Those are important topics. D must offer a solution that is both safer and more handy than the current one.

Bye,
bearophile
January 26, 2012
On Thursday, 26 January 2012 at 01:44:23 UTC, Marco Leise wrote:
> Delphi: http://delphi.about.com/od/beginners/a/delphi_set_type.htm | Scroll to: "Sets with Enumerations"
> Sets use the smallest integer type that can hold enough bits for the number of elements in an enum. So up to 8 enum flags use a byte for example. TDaySet in the example code would also be 1 byte in size. As the syntax suggests they don't implicitly convert forth or back to integers.

vote += MAX_INT for the above suggestion which btw is also the solution in Java 5.0 which provides an EnumSet type. At the moment eums are completely broken in D and IMO they are even worse that the unsafe C counterparts. C# is a lousy example here since its design is only very slightly better than the original C semantics.

I suggest looking into the Java implementation of Enum and EnumSet types which provides both type safety and convenience.

January 26, 2012
On Thu, 26 Jan 2012 00:49:58 +0100, Trass3r <un@known.com> wrote:

>> In the codebase I have to work with, having the same enum specified in
>> different places is rather common. Yeah, I hate it. This means I might
>> have a filter defined using one enum, and the value to filter being a
>> different type with the same values.
>
> Why don't you fix it then?

I'm not allowed to. The project I'm on has its tasks, and fixing things
in other projects is not one of them, especially when such an easy
workaround exists.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home