How to create friends of a class at compile time?

Tejas notrealemail at gmail.com
Thu Jul 15 18:18:28 UTC 2021


On Thursday, 15 July 2021 at 18:11:30 UTC, evilrat wrote:
> On Thursday, 15 July 2021 at 17:49:06 UTC, Tejas wrote:
>>
>> I'm sorry, I should've explicitly mentioned I'm interested in 
>> learning how to do friend injection in D.
>>
>> I know that access specifiers operate at module scope,  seen a 
>> few posts about that here already.
>> Thank you for answering though.
>
> Probably the only way is CRTP (unlikely) or mixin that access 
> internals. Both will be limited and cumbersome to use though.
>
> Additionally there is "package" visibility kind that takes 
> optional package name to give access to specific package.
>
> https://dlang.org/spec/grammar.html#attributes (under 
> visibility attributes)
>
> I rarely use package visibility so might be incorrect, but here 
> an example anyway.
> ```d
> module somepackage.somemod;
> struct A
> {
>   private int x; // only accessible from same module
>   package int y; // accessible from any module in 'somepackage'
>   package(otherpackage) int z; // accessible from 
> 'otherpackage' package
> }
> ```

CRTP and mixins are our best solution against multiple 
inheritance. Doubt they'll help with friend injection.

I also could only think of package specifiers for access control, 
but wondered whether it could be drilled down to a specific 
module, rather than package level.

you think ``` package(otherpackage.specific_module)``` is valid D 
code?

Thanks for replying, much appreciated!


More information about the Digitalmars-d-learn mailing list