Deprecate implicit conversion between signed and unsigned integers
Dom DiSc
dominikus at scherkl.de
Tue May 14 07:21:49 UTC 2024
On Tuesday, 14 May 2024 at 06:59:16 UTC, Dom DiSc wrote:
> We have a working solution that always returns the correct
> result (see https://issues.dlang.org/show_bug.cgi?id=259).
And by the way: This solution doesn't involve integer propagation
at all and also works for comparison of long with ulong.
For beginners this is by far the worst bug in D, and its there
since 18 (in words: eightteen) years - feels like lingering there
longer than D itself.
This is so distracting.
It's ten lines of code and costs nothing (except if you indeed
compare differnt signed types, and then it's still very cheep):
```d
int opCmp(T, U)(const(T) a, const(U) b) pure @safe @nogc nothrow
if(isIntegral!T && isIntegral!U && is(Unqual!T != Unqual!U))
{
static if(isSigned!T && isUnsigned!U && T.sizeof <= U.sizeof)
return (a < 0) ? -1 : opCmp(cast(U)a, b);
else static if(isUnsigned!T && isSigned!U && T.sizeof >=
U.sizeof)
return (b < 0) ? 1 : opCmp(a, cast(T)b);
else // use common type as ever:
return opCmp(cast(CommonType!(T, U))a, cast(CommonType!(T,
U))b);
}
```
More information about the dip.ideas
mailing list