On 5/18/18 12:07 PM, Gheorghe Gabriel wrote:

Sometimes, I really need to put 2-3 or more different classes in a module and I don't want them to share private members (friend classes). The current solution is to create an individual module for each class, but I don't like it when my class logic-strucrure fits better in the same module. A better solution could be private(this). I am sure that D really needs something like this. I have been talking with my friend programmers for 4 hours and they don't like the fact that D classes couldn't protect thier members in the same module. So they stuck on java and c#..

You can "simulate" this by putting the classes into their own submodules of the same package.

I.e. you want:

module mod;

class C {

 private int _x;
 int x() { return _x; }

}

void foo(C c)
{
   auto x = c._x; // Oops, this should be an error
}

You can do this via:

mod/priv.d:
module mod.priv; // name however you like

class C { ... }

mod/package.d:
module mod;
public import mod.priv; // name isn't really important here

void foo(C c)
{
   auto x = c._x; // Truly an error
}

user.d:

import mod; // imports both foo and C as if they were both in the same module

-Steve

Reply via email to