www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this expected? default to public members in private class

reply Dan Olson <zans.is.for.cans yahoo.com> writes:
It seems a private class or struct defaults to public members.  Just
curious if this is intended.  I would have expected private all the way
down unless overriden.

--- plugh.d
module plugh;
auto makeFoo() {return new Foo;}

private:

class Foo
{
    void maybepriv() {}
    private void priv() {}
    public void pub() {}
}

--- xyzzy.d
module xyzzy;
import plugh;

void main()
{
    auto f = makeFoo();
    f.maybepriv();      // is accessible after all
    //f.priv();         // err expected, member private
    f.pub();
}

--
Dan Olson
May 03 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 03 May 2015 18:07:20 -0700, Dan Olson wrote:

 It seems a private class or struct defaults to public members.  Just
 curious if this is intended.  I would have expected private all the way
 down unless overriden.
i bet it is intended. protection of struct/class members is independed of=20 protection of struct/class itself. this is good for code consistency: no=20 changing of outer protection flags can alter struct/class inner=20 machinery. so if you changed your mind about protection level of some=20 global things, you shouldn't unnecessary "fix" your code in completely=20 unrelated places.=
May 03 2015
parent Dan Olson <zans.is.for.cans yahoo.com> writes:
ketmar <ketmar ketmar.no-ip.org> writes:

 On Sun, 03 May 2015 18:07:20 -0700, Dan Olson wrote:

 It seems a private class or struct defaults to public members.  Just
 curious if this is intended.  I would have expected private all the way
 down unless overriden.
i bet it is intended. protection of struct/class members is independed of protection of struct/class itself. this is good for code consistency: no changing of outer protection flags can alter struct/class inner machinery. so if you changed your mind about protection level of some global things, you shouldn't unnecessary "fix" your code in completely unrelated places.
Thanks ketmar, that makes sense. I am in midst of adding an option to have dscanner skip private/package declarations when emitting etags (new --etags option) and I want to match what the D lang spec would say is private (if it did say). For now I have to reverse engineer based on compiler and forums, but don't want to mimic a compiler bug.
May 04 2015