alias this and opDot()
user1234
user1234 at 12.de
Wed Sep 28 06:39:30 UTC 2022
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 at 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));
}
```
More information about the Digitalmars-d
mailing list