anonymous structs within structs

thinkunix thinkunix at zoho.com
Mon Dec 4 23:16:27 UTC 2023


DLearner via Digitalmars-d-learn wrote:
> On Monday, 4 December 2023 at 21:55:29 UTC, Mike Shah wrote:
> 
> [...]
>>
>> Is something like this what you had in mind?
>>
>> ```
>> void main() {
>>    import std.stdio;
>>
>>    mixin template A() {
>>       int I1;
>>       int I2;
>>       char X;
>>    }
>>
>>    struct B {
>>       mixin A;
>>       int Var1;
>>       int Var2;
>>    }
>>
>>         B someObject;
>>         writeln(someObject.I1);
>>         writeln(someObject.I2);
>> }
>> ```
> 
> More like
> ```
> B someObject;
>           writeln(someObject.Var1);
>           writeln(someObject.Var2);
> ```
> 
> In the areas where B is used, don't want I1 or I2 to be visible.
> 

If you don't want members of A to be visible in B, why are you
even including struct A as part of B?

Why wouldn't you use a class and make the members private?

I'm certainly not a D expert and I don't know your use case
so maybe I'm missing something.

scot


More information about the Digitalmars-d-learn mailing list