Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:13:30+00:00 2026-05-22T22:13:30+00:00

import std.stdio; struct Vector2 { float x, y; this (float x, float y) {

  • 0
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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:13:30+00:00Added an answer on May 22, 2026 at 10:13 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in D import std.stdio; class Thing { // Fields
How can you automatically import the latest build/revision number in subversion? The goal would
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
In python you can do something like this to import a module using a
import java.io.*; public class Auto { /** * @param args * @throws IOException */
import Crypto.Cipher.DES import struct def rol32(x, y): ret = ((x<<y)&0xFFFFFFFF)|((x>>(32-y))&0xFFFFFFFF) #print 'rol32', hex(x), hex(y),
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import java.io.FileReader; public class Main { public static void main(String[]
import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
I need to import a csv file into Firebird and I've spent a couple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.