Created attachment 1215
compile error for small number types
I would expect that it is allow to use the operator + and -.
on byte,ubyte,short and ushort numbers by dmd (and gdc) convert those to an
int.
The attach example gives the following complier error.
short_int_test.d(8): Error: cannot implicitly convert expression (cast(int)n +
cast(int)k) of type int to ubyte
When the Num alias is ubyte,byte,ushort and short.
So if we what to use those type we have to cast to an int isn't this a little
overkill.
So instead of
r=n+k;
We have to write.
r=cast(Num)(n+k);
Where r,n,k is the to of Num.
Comment #1 by bearophile_hugs — 2013-05-21T12:21:19Z
I am compiling this program, and I don't see errors or warnings:
void main() {
alias Num = uint;
Num n = 12;
Num k = 11;
Num r;
r = n - k;
}
Comment #2 by schveiguy — 2013-05-21T12:42:53Z
The CPU does math at an integer level. So what happens is, short, ushort, byte, and ubyte are integer promoted to int, then the operation is performed. After the operation, the number may or may not fit into the short, ushort, byte, or ubyte. So the compiler requires a cast so you can verify "yes, I know I'm throwing away data."
This is working as expected.
(In reply to comment #1)
> I am compiling this program, and I don't see errors or warnings:
Right, the report states you have to change Num to byte, ubyte, short, or ushort. Probably the attachment should be one of those.