PhobosWatch: manifest => enum

Walter Bright newshound1 at digitalmars.com
Sat Dec 29 17:47:03 PST 2007


Bruce Adams wrote:
> 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;
> };

Yes, it is illegal.

> Why opAdd and not opIncrement?

opIncrement is redundant, as it's a subset of opAdd.

> 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.

I don't know why it would be unnatural. To me, a type that can be 
incremented but not added would seem very strange indeed.


> 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++;

A string cannot be incremented.

>   }
> }
> 
> 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