alias this private?

js.mdnq js_adddot+mdng at gmail.com
Sun Dec 9 00:30:29 PST 2012


On Sunday, 9 December 2012 at 08:27:45 UTC, Ali Çehreli wrote:
> On 12/08/2012 11:05 PM, js.mdnq wrote:
> > Can it not be possible to use alias this on a private field?
>
> I've just tested it. It is possible with dmd 2.060:
>
> struct Q
> {
> private:
>     int _x;
> public:
>     alias _x this;
> }
>
> void main()
> {
>     auto q = Q();
>     q = 42;
>     assert(q._x == 42);
> }
>
> > struct Q
> > private:
> > int _x;
> > public:
> > alias _x this;
> >
> > ?
> >
> > Seems to me that one does not always want the user to access
> the
> > internal value that is aliased ;/
>
> Then the programmer should not "alias this" that member.
>
> > Q q;
> >
> > q._x // error yet q, for all practical purposes is _x,
>
> q is for /some/ practical purposes is _x. :)
>
> > we just stop the
> > user from being able to directly access it.
>
> That is the case, because the member is private.
>
> > For example, maybe I want to override opAssign on an int.
>
> Sorry, I could not understand that.
>
> > The user can
> > get around it by doing q._x. Seems like a bad idea.
>
> The user can't do that because _x is private. If they can, then 
> it must be a bug. One thing that is confusing in D is that 
> private members of a type are accessible by the module that 
> includes that type. Is that the question?
>
> Ali

No, it doens't work on my version of D(2.058 I think). So maybe 
ok now.


More information about the Digitalmars-d-learn mailing list