December 10, 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

December 10, 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...
December 10, 2012
On 12/10/2012 01:56 PM, js.mdnq wrote:

> 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?)

I may have been too subtle before but opGet() is not an operator function. Although its name starts with op, it is still a member function.

> which is
> different than aliasing a type?

The way I understand it, the compiler looks at the return type of that function and uses that function when the object is used instead of that type. If 'int foo()' is used in alias this, because foo() returns int, the object is eligible to be used in place of an int. foo() gets called to produce that int value.

Ali

1 2
Next ›   Last »