Emulating enums

JS js.mdnq at gmail.com
Thu Aug 1 03:29:39 PDT 2013


On Thursday, 1 August 2013 at 05:22:46 UTC, Ali Çehreli wrote:
> On 07/31/2013 06:10 PM, JS wrote:
>> http://dpaste.dzfl.pl/dbb40dbc
>>
>> The code was pared down from a lot of string mixin code 
>> generation. I
>> nest the structs because I want a nested enums. I don't want 
>> have to
>> have eState and eStateType but eState and eState.Type.
>>
>> Having the ability to nest enums would solve my problem.
>>
>> Regardless, I can almost achieve the effect with nested 
>> structs but not
>> quite.
>>
>> In the code, I cannot assign to the struct for some reason 
>> even with
>> alias this on iB, which should make State act like the int 
>> Value.
>>
>> i.e.,
>>
>> b.State.Value = Enums.State.A;
>>
>> works but
>>
>> b.State = Enums.State.A;
>>
>> doesn't
>>
>> It maybe some stupid error on my part but I can't keep my eyes 
>> open
>> enough to figure it out...
>>
>>
>>
>
> For that assignment to work, the left-hand side must be 
> assignable. However, the property function State() returns 
> Enums.eState by-value.
>
> The following has the same issue:
>
> struct S
> {
>     int i_;
>
>     @property int i() {
>         return i_;
>     }
>
>     alias i this;
> }
>
> void main()
> {
>     auto s = S();
>     s = 42;
>     assert(s.i == 42);
> }
>
> Error: cannot implicitly convert expression (42) of type int to 
> S
>
> To compile, i() must return an lvalue:
>
>     @property ref int i() {
>
> Ali

Thanks...


More information about the Digitalmars-d-learn mailing list