Super easy struct construction question that I'm embarrassed to ask.

Salih Dincer salihdb at hotmail.com
Fri Jan 10 01:39:58 UTC 2025


On Thursday, 9 January 2025 at 22:01:59 UTC, WhatMeWorry wrote:
> produces:
> n = Node(Location(0, 0), 33)
> when I expected
> n = Node(Location(1, 2), 33)

This is a simple typo (it shouldn't be 1 letter) but the code 
should be made smarter thanks to the capabilities of D. No more 
fear of writing like C and getting lost like C++. For example:

```d
struct Location
{
   int x, y;

   auto opBinary(string op = "*")(uint m)
   {
     x *= m;
     y *= m;

     return this;
   }
}

import std.stdio;
void main()
{
   auto loca = Location(1, 2);
   auto node = Node(loca, 33);

   node.writefln!"n = %s";
}

struct Node
{
   uint multiplier;
   Location location;

   this(Location location, uint multiplier)
   {
     this.location = location * multiplier;
     this.multiplier = multiplier;
   }

}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list