is private broken?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Oct 10 19:59:05 UTC 2017


On Tuesday, October 10, 2017 19:50:49 Jonathan Marler via Digitalmars-d 
wrote:
> On Tuesday, 10 October 2017 at 19:33:30 UTC, Steven Schveighoffer
>
> wrote:
> > On 10/10/17 3:20 PM, Jonathan Marler wrote:
> >> On windows I was able to compile the following using both
> >> dmd.2.075.1 and dmd.2.076.1
> >>
> >>  From what I understand, you shouldn't be able to access
> >>
> >> private fields/methods like this...am I missing something?
> >
> > Before I even read your code, I was pretty sure the error :)
> >
> > private is module-based, not type based. Put your structs in a
> > different module, and private will work.
> >
> > -Steve
>
> Wow I can't believe I've gone this long not realizing that.  I'm
> surprised there's not a way to make fields private to their own
> struct/class.

There is - by putting them in a separate module. It was done this way to
avoid the extra complication of friend functions that you get in C++, and
the theory is that you're in control of everything that's in your own
module, so you should be able to deal with not having stuff access stuff in
the same module when it shouldn't. And if you want nothing outside the type
to have access, just stick the type in its own module.

- Jonathan M Davis



More information about the Digitalmars-d mailing list