Search code examples
operator-overloadingd

D operator overloading


import std.stdio;

struct Vector2
{
    float x, y;

    this (float x, float y)
    {
        this.x = x;
        this.y = y;
    }

    // vector2 * number
    Vector2 opBinary(string op)(const float rhs)
    if (op == "*")
    {
        auto result = this;
        this *= rhs;
        return this;
    }

    // number * vector2
    Vector2 opBinaryRight(string op)(const float lhs)
    if (op == "*")
    {
        return this.opBinary!(op)(lhs);
    }

    /*
      assignment operators
    */

    // vector2 = vector2
    ref Vector2 opAssign(const ref Vector2 rhs)
    {
        x = rhs.x;
        y = rhs.y;
        return this;
    }

    // vector2 *= number
    ref Vector2 opOpAssign(string op)(const float rhs)
    if (op == "*") {
        x *= rhs;
        y *= rhs;
        return this;
    }
}

unittest
{
    auto first = Vector2(1, 2);
    auto second = Vector2(3, 3);
    auto number = 4.0f;

    Vector2 result = first *= 3;
    assert(result == Vector2(3, 6));
    // BUG *
    // assert(first == Vector2(1, 2));    
}

void main() 
{}

Hi. When I try to compile this small program with -unittest option why does the last assertion fails? Any help would be appreciated. Thanks..


Solution

  • Why would you expect it to pass?

    first *= 3 modifies first, so it doesn't retain its original value.

    Perhaps you meant to write

    Vector2 result = first * 3;
    

    ?

    There is also a problem with Vector2 opBinary(string op)(const float rhs)

    That function is what is used in expression like 10 * v. Your code modifies this in the expression this *= rhs. That function should be implemented:

    auto result = this;
    result *= rhs;
    return result;