www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - alias this and opDot()

reply Salih Dincer <salihdb hotmail.com> writes:
Ok, opDot() is deprecated in version 2.082. However, if we 
simulate this situation with a function (opdot in our example), 
we get unexpected results.

```d
struct Point { int x, y; }

struct S
{
     Point point;
     alias opdot this;

     auto opdot()
     { // opDot() is deprecated in v2.082
       //this.point = Point(100, 100);/* <-- also if you only use: 
s == Point(100, 101)
         point.x = 100;
         point.y = 100;
         point.x++;//*/

         return &point;
     }
}

void main()
{
     S s;
     s.x++; // as if this line doesn't exist!
     s.y++;

     //s.x++; //  <-- also if you use: s == Point(102, 100)
     assert(s.point == Point(101, 101));
}
```

SDB 79
Sep 27 2022
parent reply user1234 <user1234 12.de> writes:
On Wednesday, 28 September 2022 at 03:45:59 UTC, Salih Dincer 
wrote:
 Ok, opDot() is deprecated in version 2.082. However, if we 
 simulate this situation with a function (opdot in our example), 
 we get unexpected results.

 ```d
 struct Point { int x, y; }

 struct S
 {
     Point point;
     alias opdot this;

     auto opdot()
     { // opDot() is deprecated in v2.082
       //this.point = Point(100, 100);/* <-- also if you only 
 use: s == Point(100, 101)
         point.x = 100;
         point.y = 100;
         point.x++;//*/

         return &point;
     }
 }

 void main()
 {
     S s;
     s.x++; // as if this line doesn't exist!
     s.y++;

     //s.x++; //  <-- also if you use: s == Point(102, 100)
     assert(s.point == Point(101, 101));
 }
 ```

 SDB 79
opdot is called two times so you reset the state of point members 100 ```d struct Point { int x, y; } struct S { Point point; alias opdot this; Point* opdot() return { this.point.x = 100; this.point.y = 100; return &this.point; } } void main() { S s; s.x += 1; assert(s.point == Point(101, 100)); s.y += 1; assert(s.point == Point(100, 101)); } ```
Sep 27 2022
next sibling parent user1234 <user1234 12.de> writes:
On Wednesday, 28 September 2022 at 06:39:30 UTC, user1234 wrote:
 On Wednesday, 28 September 2022 at 03:45:59 UTC, Salih Dincer 
 wrote:
 Ok, opDot() is deprecated in version 2.082. However, if we 
 simulate this situation with a function (opdot in our 
 example), we get unexpected results.

 ```d
 struct Point { int x, y; }

 struct S
 {
     Point point;
     alias opdot this;

     auto opdot()
     { // opDot() is deprecated in v2.082
       //this.point = Point(100, 100);/* <-- also if you only 
 use: s == Point(100, 101)
         point.x = 100;
         point.y = 100;
         point.x++;//*/

         return &point;
     }
 }

 void main()
 {
     S s;
     s.x++; // as if this line doesn't exist!
     s.y++;

     //s.x++; //  <-- also if you use: s == Point(102, 100)
     assert(s.point == Point(101, 101));
 }
 ```

 SDB 79
opdot is called two times so you reset the state of point members 100 ```d struct Point { int x, y; } struct S { Point point; alias opdot this; Point* opdot() return { this.point.x = 100; this.point.y = 100; return &this.point; } } void main() { S s; s.x += 1; assert(s.point == Point(101, 100)); s.y += 1; assert(s.point == Point(100, 101)); } ```
I realize that maybe you are aware of what's happening, in which case you need to use opDispatch instead ```d struct Point { int x, y; } struct S { Point point; auto ref opDispatch(string member)() return { return __traits(getMember, point, member); } } void main() { S s; s.x += 1; assert(s.point == Point(1, 0)); s.y += 1; assert(s.point == Point(1, 1)); } ```
Sep 27 2022
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Wednesday, 28 September 2022 at 06:39:30 UTC, user1234 wrote:
 opdot is called two times so you reset the state of point 
 members 100
```d void main() { S s; s.x++; /* In this line, it's x = y = 100 firstly, then x = 101 and 102. */ s.y++; /* In this line, it's x = y = 100 again, then x = 101 and y = 101. */ assert(s.point == Point(101, 101)); // That's ok... } ``` Thank you... It seems that I'm as sharp as a tack. 😀 SDB 79
Sep 28 2022