www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - alias function this - limitations

reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
struct Ref( T ) {
   T* payload;
    property ref T getThis( ) {
     return *payload;
   }
    property ref T getThis( ref T value ) {
     payload = &value;
     return *payload;
   }
   alias getThis this;
}

void bar( ) {
   Ref!int f;
   int n;
   f = n;
   int b = f;
}

This code fails on the line 'int b = f;'. Is it supposed to?

-- 
Simen
Jun 24 2010
next sibling parent reply div0 <div0 users.sourceforge.net> writes:
On 24/06/2010 13:52, Simen kjaeraas wrote:
 struct Ref( T ) {
 T* payload;
  property ref T getThis( ) {
 return *payload;
 }
  property ref T getThis( ref T value ) {
 payload = &value;
 return *payload;
 }
 alias getThis this;
 }

 void bar( ) {
 Ref!int f;
 int n;
 f = n;
 int b = f;
 }

 This code fails on the line 'int b = f;'. Is it supposed to?

I think so. 'alias this' is used to forward stuff that appears to the right of a dot onto the named member. In c++ to get what you are doing to work, you'd add a customer cast operator: http://www.digitalmars.com/d/2.0/operatoroverloading.html#Cast but I'm not sure how the cast operator overloading works in D, I don't know if it will try to implicitly cast and call your operator. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jun 24 2010
parent div0 <div0 users.sourceforge.net> writes:
On 24/06/2010 19:23, Simen kjaeraas wrote:
 div0 <div0 users.sourceforge.net> wrote:

 This code fails on the line 'int b = f;'. Is it supposed to?

I think so. 'alias this' is used to forward stuff that appears to the right of a dot onto the named member.

Not only. Assignment of the wrapped type will also work.

Yes, I can see your intent and I think it should probably be made to work that way. I've been having a play and as far as I can tell it looks like aliasing this to a method just doesn't work at all; I think the alias this is being completely ignored.
 I feel that alias this is currently broken, but hope it will be fixed.

It will be given time. We also could do with multiple alias this allowed, would make porting certain styles of c++ template code much easier. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jun 24 2010
prev sibling next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
div0 <div0 users.sourceforge.net> wrote:

 This code fails on the line 'int b = f;'. Is it supposed to?

I think so. 'alias this' is used to forward stuff that appears to the right of a dot onto the named member.

Not only. Assignment of the wrapped type will also work.
 In c++ to get what you are doing to work, you'd add a customer cast  
 operator:

 http://www.digitalmars.com/d/2.0/operatoroverloading.html#Cast

 but I'm not sure how the cast operator overloading works in D, I don't  
 know if it will try to implicitly cast and call your operator.

It does not. In WalterAndrei.pdf, opImplicitCast(To|From) is mentioned, but it has not found its way into the language. I feel that alias this is currently broken, but hope it will be fixed. -- Simen
Jun 24 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 24 Jun 2010 14:23:09 -0400, Simen kjaeraas  
<simen.kjaras gmail.com> wrote:

 It does not. In WalterAndrei.pdf, opImplicitCast(To|From) is mentioned,
 but it has not found its way into the language.

Alias this is supposed to implement what opImplicitCast was supposed to implement. opImplicitCast is not going to be implemented. I agree it's not quite there yet. -Steve
Jun 24 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
div0 <div0 users.sourceforge.net> wrote:

 I've been having a play and as far as I can tell it looks like aliasing  
 this to a method just doesn't work at all; I think the alias this is  
 being completely ignored.

It would seem you're right. Just that assignment made me think it did work. Now why does the assignment work? It certainly does not if no alias this is present. -- Simen
Jun 24 2010