Why is this legal?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 29 03:05:22 PDT 2017


On Wednesday, March 29, 2017 10:56:34 rikki cattermole via Digitalmars-d-
learn wrote:
> On 29/03/2017 10:50 AM, abad wrote:
> > This works:
> >
> > class Foo {
> >
> >     protected void bar() {
> >
> >         writeln("hello from foo");
> >
> >     }
> >
> > }
> >
> > void main() {
> >
> >     auto foo = new Foo;
> >     foo.bar();
> >
> > }
> >
> > Is this on purpose and what's the rationale?
>
> http://dlang.org/spec/attribute.html#visibility_attributes
>
> "protected only applies inside classes (and templates as they can be
> mixed in) and means that a symbol can only be seen by members of the
> same module, or by a derived class. If accessing a protected instance
> member through a derived class member function, that member can only be
> accessed for the object instance which can be implicitly cast to the
> same type as ‘this’. protected module members are illegal."

Yeah, everything in a module can see everything else in a module. It avoids
needing to add the complication of friend function and classes like in C++.
Basically, it's like everything within a module were declared as friends. If
you want something to not have access to something else, then it's going to
need to be put in a separate module.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list