How to create friends of a class at compile time?

evilrat evilrat666 at gmail.com
Thu Jul 15 18:11:30 UTC 2021


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
}
```




More information about the Digitalmars-d-learn mailing list