using the full range of ubyte with iota

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 25 06:11:09 PST 2015


On Sunday, 25 January 2015 at 13:03:16 UTC, bearophile wrote:
> Dominikus Dittes Scherkl:
>
>> Because this is useful in more situations,
>
> Right, but it's still a cast. And in D you want to minimize the 
> number of usages of casts. The proposed syntax iota!"[]" is 
> cast-safe.
>
I don't case too much, if I have ensured the cast is safe by 
constraints beforehand.

I need to cast often anyway, because I work with small types and 
most operators permanently change everything to "int", especially 
the bit-operations for which a signed type makes no sense at all:

ubyte x = 50;
auto y = x & 0x11; // y is int! I hate that!

even if I use unsigned literals:
auto z = x & 0x12u; // z is uint - better but still bad. More so 
as & should result in the smaller of the two types!!

But I need not even use literals (which unfortunately cannot be 
makred as "ubyte" or "short"). Look at this:

auto x2 = (x>>4) | (x<<4); // swap nibbles - but result in an 
int!!!!!


More information about the Digitalmars-d-learn mailing list