D operator overload

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() 
{}

Hey. When I try to compile this little program with the -unittest option , why doesn't the last statement fail? Any help would be greatly appreciated. Thank..

+3
source share
1 answer

Why do you expect it to pass?

first *= 3changes first, so it does not retain its original meaning.

Perhaps you wanted to write

Vector2 result = first * 3;

?

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

This function is used in a type expression 10 * v. Your code changes thisin the expression this *= rhs. This function must be implemented:

auto result = this;
result *= rhs;
return result;
+5
source

All Articles