Negating a short?

Salih Dincer salihdb at hotmail.com
Wed Nov 6 16:48:54 UTC 2024


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:

```d
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 at 79




More information about the Digitalmars-d-learn mailing list