Thread overview
alias this and opDot()
Sep 28, 2022
Salih Dincer
Sep 28, 2022
user1234
Sep 28, 2022
user1234
Sep 28, 2022
Salih Dincer
September 28, 2022

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.

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

September 28, 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.

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

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));
}
September 28, 2022

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.

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

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

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));
}
September 28, 2022

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

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