On Tuesday, 5 November 2024 at 17:32:00 UTC, Andy Valencia wrote:

integral promotion not done for -val

```d
I ended up with this, but is negating a short really this problematic, or did I miss something?

    static if (!__traits(isUnsigned, T)) {
        if (val < 0) {
static if (__traits(getPointerBitmap, T)[0] < int.sizeof) {
                val = cast(T)(-(cast(int)val));
            } else {
                val = -val;
            }
        }
    }

```

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

```d
void main()
{
  Short foo = { -21 };
  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

Reply via email to