alias this private?

Ali Çehreli acehreli at yahoo.com
Sun Dec 9 23:48:36 PST 2012


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



More information about the Digitalmars-d-learn mailing list