On Tuesday, 11 December 2012 at 13:24:59 UTC, d coder wrote:
No, it's a fix of a gotcha from C. The C code would just allow the
assignment.


Yes Andrei.

But it does not look clean if you have to write:

byte a, b, c;
a = cast(byte) (b + c);

Well I know the advantages (safety). But imagine having to write all that when working with bytes and shorts. Makes it really difficult to work with shorts and bytes in D. Would I be asking for too much if I ask DMD to provide a compiler flag that makes it return bytes and shorts for operations on them? So the deal would be, if you use this compiler flag,
code behavior would be different from that of C/C++.

What we get would be more practical short and byte behavior.

Regards
- Puneet

If the compiler can statically verify that some integer value fits in a byte or short, you don't need the cast. So to rewrite your example:

byte a, b, c;
a = (b + c) & 0xFF;

Also:
short a;
byte b, c;
a = b + c;

Still not exactly what you're looking for, but the mask looks nicer than a cast IMO. It's also safer.

Reply via email to