alias this private?

js.mdnq js_adddot+mdng at gmail.com
Mon Dec 10 13:56:54 PST 2012


On Monday, 10 December 2012 at 07:48:37 UTC, Ali Çehreli wrote:
> On 12/09/2012 10:43 PM, js.mdnq wrote:
>
> > I thought `alias this` essentially treats the object as the
> alias.
> >
> > struct A {
> > alias value this;
> > int value;
> > void func();
> > }
> >
> > A a;
> >
> > then a is essentially the same as a.value?
>
> No. 'alias value this' means "when this object is used in a 
> context where the type of 'value' is expected, then use the 
> 'value' member instead."
>
> In your example above, it means "when A is used in place of 
> int, use 'value' instead." It is basically for automatic type 
> conversion.
>
> (A reminder: Although the language spec allows multiple alias 
> this declarations, current dmd supports only one.)
>
> > a.func() would be
> > a.value.func() which makes no sense?
>
> a.func() would still call A.func on object 'a' because 'a' is 
> an A. Only when 'a' is used as an int, 'value' is considered.
>
> > At least that is how I thought
> > alias this worked? (it's obviously more complex than what I
> thought)
>
> Perhaps it is simpler than what you thought. :)
>
> > (I'm new to D so you'll have to forgive all the stupid
> questions ;)
>
> Your questions make all of us learn more. :)
>
> > D seems quite powerful and many useful ways to do things but
> I still
> > haven't wrapped my head around all the intricacies)
>
> Indeed... There are a lot of features.
>
> Ali

Yeah, basically it's close to what I was thinking but I guess the 
opGet, which I'm still not familiar with, behaves a bit different 
when aliased. Possibly because we are aliasing a operator(or 
method?) which is different than aliasing a type?

e.g., `alias opAssign this;` makes no sense to me ;/

Thanks for the help...


More information about the Digitalmars-d-learn mailing list