Using . notation abstractly

Simen Kjærås simen.kjaras at gmail.com
Fri Oct 12 13:24:00 UTC 2018


On Friday, 12 October 2018 at 12:43:53 UTC, Paul Backus wrote:
> On Wednesday, 10 October 2018 at 22:56:14 UTC, James Japherson 
> wrote:
>> The point of all this is because D does not allow nesting of 
>> enums
>>
>> which allows for nice use of . to separate hiearchies:
>>
>> enum A
>> {
>>    enum B
>>    {
>>       X,
>>    }
>> }
>>
>> A.B.X, rather than having to have one large flat enum and do 
>> things like A_B_X.
>
> You can use a mixin template to introduce a new namespace:
>
> https://run.dlang.io/is/K0kJJl

True, but D has namespaces elsewhere, which don't require you to 
clutter some other namespace with your enums:

struct A {
     enum B {
         X,
     }
}

final abstract class C {
     enum D {
         Y,
     }
}

This has the added benefits of 1) being more obvious, and 2) you 
can put other stuff in there.

On Wednesday, 10 October 2018 at 22:56:14 UTC, James Japherson 
wrote:
> I know one can use structs but it is messy and not general 
> enough.

Please do elucidate - what do you mean not general enough? When 
is a struct less general than an enum?


> struct A
> {
>     alias Dispatch this;
> }
> 
> A.B.X
> 
> but it requires more machinery to setup.

Also:

alias A = Dispatch;
A.B.X;


Note that this results in Dispatcher!("B", "X"), so you'll have 
to pass the "A" manually (the same problem exists in your code 
above):

alias A = Dispatcher!"A";
A.B.X; // Dispatcher!("A", "B", "X")

--
   Simen


More information about the Digitalmars-d-learn mailing list