Request: nested enums

Yota yotaxp at
Tue Jul 16 08:50:53 PDT 2013

On Monday, 15 July 2013 at 13:47:21 UTC, JS wrote:
> Here is a better example that can actually be implemented but 
> not pretty:
> final immutable struct Msg
> {
>     immutable int Foo = 0;
>     immutable int Bar = 1;
>     final immutable struct Type
>     {
>         immutable int Error = 10000;
>         immutable int Ok = 10001;
>     }
> }
> Here type is not necessarily a logical subtype of Msg but 
> better stated as Msg.Type.Error rather than MsgType.Error.
> If a flag distribution was used, we could do Msg.Foo || 
> Msg.Type.Error.
> In any case generating such a structure is a bit of a pain 
> compared to a normal enum.

That could cause a bit of readability issue.  If you know Msg is
an enum, and then see in the code Msg.Foo, Msg.Bar, and Msg.Type,
any programmer is gonna instantly assume that Msg.Type is just
another value.  If they see Msg.Type.Error, and are still certain
that Msg is an enum, they they might reason that the enum is of a
user defined type, and you're reading the Error field/property.
Either way, it seems a bit counterproductive.

I do see the appeal of nesting types though, as I dislike dumping
things into the global namespace, so I might advise something
like this:

struct Msg {
	enum Type { Foo, Bar }
	enum Result { Error, OK }

Be careful with ORing values from different enums though, as the
result type won't be an enum.

More information about the Digitalmars-d mailing list