anonymous structs within structs

DLearner bmqazwsx123 at gmail.com
Mon Dec 4 23:46:45 UTC 2023


On Monday, 4 December 2023 at 23:16:27 UTC, thinkunix wrote:
> 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


Basically, B corresponds to the whole record (and only a whole 
record can be read).
But the task only requires Var1 and Var2, the last two fields on 
the record.
By putting all the irrelevant fields into A, and defining B as 
above,
program remains unpolluted with data it does not need.





More information about the Digitalmars-d-learn mailing list