Operator declaration

Dom Disc dominikus at scherkl.de
Sun May 31 23:53:27 UTC 2026


On Sunday, 31 May 2026 at 15:42:32 UTC, Nick Treleaven wrote:
> On Sunday, 31 May 2026 at 08:54:39 UTC, Dom Disc wrote:
>> On Friday, 29 May 2026 at 10:56:51 UTC, Nick Treleaven wrote:
>>> Can you give an example of what you think should work but 
>>> doesn't?
>>
>> ```d
>> byte x = 7;
>> x = -x;  // cannot convert -x of type int to byte
>> ```
>
> At the point of `-x`, the compiler doesn't know that `x != 
> x.min`, so VRP correctly prevents this. Do you think the 
> compiler should allow it? How would it decide that? (If `x` is 
> const, it can be assigned to a byte).

-byte.min == byte.min (twos complement - also the same result as 
if the int-value 128 is truncated to signed eight bit), so yes, 
this should be allowed.  -x should always have same type as x.

But I would prefer that T.min is NaN of signed types. Instead 
byte.min should be -127 (or general T.min == -T.max). This would 
also fix abs().




More information about the Digitalmars-d-learn mailing list