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

  • SEARCH
  • Home
  • 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
  • 2 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

Hello World for D looks like this: import std.stdio; void main(string[] args) { writeln(Hello
Testing the following in D import std.stdio; struct S { int _val; @property ref
std.process has a nice shell() function. import std.process; import std.stdio; void main() { string
Linking the following two files gives me a link-error: a.d: import std.stdio; extern string
I have the following code in D import std.stdio; class Thing { // Fields
i did this code : from scitools.std import * npoints=10 vectorpoint=array(random.uniform(-1,1,[1,2])) experiment=array(random.uniform(-1,1,[npoints,2])) print(vectorpoint=,vectorpoint) print(experiment=,experiment)
I did this code: from scitools.std import * from sympy import * x=Symbol('x') #Integral
Consider the following code: module ftwr; import std.regex; import std.stdio; import std.conv; import std.traits;
import std.stdio; void main(){ int n; while(readf("%d", &n)){ if(n == 11) break; writeln(n); }
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }

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.