private keyword dont appear to do anything

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Nov 3 20:01:27 UTC 2017


On Friday, November 03, 2017 13:43:15 rikki cattermole via Digitalmars-d-
learn wrote:
> Visibility modifiers like private, and public are to the module not the
> scope.
>
> "Symbols with private visibility can only be accessed from within the
> same module."
>
> This is how module based languages work, a bit more useful then to the
> scope approach IMO. An easy mistake to make.
>
> https://dlang.org/spec/attribute.html#visibility_attributes

To be fair, there are times where it would arguably be desirable to restrict
private to a struct or class, so it's not all wonderful, but it does mean
that we don't have to have the extra complication of friend functions like
C++ does (which I believe is the biggest motivation for it). And if a module
is so large that you can't make sure that nothing is accessing private
struct or class members when it shouldn't, then the module is probably
overly large and/or complicated.

Most folks are surprised by this behavior, and occasionally, they complain
about it, but in practice, the only real problem that I'm aware of is that
you can sometimes write unit tests which work just because they're in the
module, and the code doesn't work when used outside of the module. But even
that tends to be only an occasional problem.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list