core.checkedint added to druntime
bearophile via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Wed Jun 18 16:02:39 PDT 2014
Walter Bright:
> Because there is no ubyte/byte/short/ushort math in C, C++ or
> D. There is only int/long math.
A little of ubyte math is present, perhaps for this you add
"uinc", "sinc", "udec", "sdec" functions to core.checkedint that
support ubyte/byte/short/ushort types too:
void main() {
ubyte x = 100;
x++;
}
But you are right, in D if you sum two ubytes you get an int:
void main() {
ubyte x = 100;
ubyte y = 200;
pragma(msg, typeof(x + y)); // Prints "int"
}
Yet sometimes you want to perform safe math operations on
ubytes/bytes/shorts/ushorts, and keep the same type for the
results. So I presume a future Phobos Safe math based on
core.checkedint will need to check the ranges by itself, to allow
checked assignments back to the same types:
void main() {
Safe!ubyte x = 100;
Safe!ubyte y = 200;
Safe!ubyte z = x + y;
}
Bye,
bearophile
More information about the Digitalmars-d-announce
mailing list