8 hours ago

On Wednesday, 6 November 2024 at 16:38:40 UTC, Salih Dincer wrote:

>

In response to Andy and Matheus, I think implementing your own type might be a solution:

Oh, am I too hasty? There was already an s in the test environment, so I thought 2 overloads were unnecessary. I don't have a programmer or anything, my brother :)

In fact, it would be nice if there was an option to correct the threads we wrote on our forum. At least for a certain period of time. Here is the latest code that works:

void main()
{
  Short foo = { 21 };

  foo *= -1;

  foo = foo * -2;
  assert(foo.s == 42);
}

struct Short
{
  short s;

  auto opBinary(string op: "*")(int rhs)
  {
    auto result = s * rhs;
    return Short(cast(short)result);
  }

  void opOpAssign(string op: "*")(int rhs)
  {
    s *= rhs;
  }
}

SDB@79


6 hours ago
On Wednesday, 6 November 2024 at 16:48:54 UTC, Salih Dincer wrote:
> On Wednesday, 6 November 2024 at 16:38:40 UTC, Salih Dincer wrote:
>> In response to Andy and Matheus, I think implementing your own type might be a solution:
>
> ...
>

Thanks for the info and the code.

Matheus.
6 hours ago
On Wednesday, 6 November 2024 at 16:27:22 UTC, Dennis wrote:
> On Wednesday, 6 November 2024 at 11:51:41 UTC, Matheus wrote:
>> Shouldn't these two act the same?
>
> That would make sense, but you wouldn't make a special case just for that specific expression, and it's hard to find a good rule that generalizes to all expressions.

I thought that internally:

i *= -1;

Would be expanded as:

i = i * -1;

And from the expanded version it would generate the same Assembly/Machine Code and follow the same rules.

Thanks,

Matheus.
1 2
Next ›   Last »