On Sun, 25 Jan 2015 14:11:09 +0000, Dominikus Dittes Scherkl wrote: > 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!!!!!
this is true for C and C++ too, as all three languages doing "integer promotion". the only difference is that D forbids potentially lossy assigns. you best bet is to not use `auto`, but specify required type explicitly. or use ints/uints and cast to bytes only when it is necessary.
signature.asc
Description: PGP signature