PhobosWatch: manifest => enum

Bruce Adams tortoise_74 at yeah.who.co.uk
Sat Dec 29 04:59:21 PST 2007


On Sat, 29 Dec 2007 08:59:46 -0000, Walter Bright  
<newshound1 at digitalmars.com> wrote:

>
> Let me enumerate (!) the enhancements to enum:
>
> 1) The enum 'base type' is no longer restricted to being integral types  
> only.
> 2) Members of anonymous enums can now be of heterogeneous types, the  
> types being deduced from their initializers.
> 3) .init, .min and .max have no meaning for anonymous enums, and so are  
> computed only for tagged enums.
> 4) For anonymous enum members, a type can prefix the identifier as a  
> convenience.
> 5) If there is only one member in an anonymous enum, the { } can be  
> omitted.
> 6) If .init, .min, .max or 'next' values are not required, then the base  
> type doesn't have to support the operations required to produce those  
> values.
>
> None of these are takeaways.

Personally I always name my types but there may be those that don't.
Is this currently illegal then?

class Colour
{
private:
   // private helper type defining colour state variable
   // using an anonymous enum.
   enum { red, green, blue } colour;
};

> Don Clugston wrote:
>>  Having an enum automatically get the 'next' value is one of the key  
>> feature of enums, and it relies on the base type being an enumerable  
>> type.
>
> The only thing it relies on is the ability to add 1 to the previous  
> value. The new enums can automatically get the next value for any type  
> with this characteristic, including UDT's that overload opAdd, as long  
> as they can be evaluated at compile time.
>
Why opAdd and not opIncrement?

opAdd(int) seems unnatural for user defined types. They would have to  
ignore the
argument and it would lead to some odd bugs and confusions.

Very contrived and poorly chosen example:

class Foo
{
public:
   // helper type
   enum FooType
   {
     A = "foo",
     B = "bar",
     C = "snafu"
   }
private:
   // state - bar may be one o
   string Bar;

public:
   Foo()
   {
     Bar = A;
   }

   // only used to allow creation of Foo based enums.
   Foo opAdd(int)
   {
     Bar++;
   }
}

enum FooBar: Foo
{
    A = Foo("foo"),
    B = Foo("bar"),
    C = Foo("snafu")
}

FooBar a = FooBar.A;
FooBar b = FooBar.B;
FooBar c = FooBar.C;

assert(b == (a+1)); // okay
assert(c == (b+1)); // okay
assert(b == (a+2)); // surprising!




More information about the Digitalmars-d mailing list