Can't add ubytes together to make a ubyte... bug or feature?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 19 15:07:24 PST 2016
On 01/19/2016 02:12 PM, Soviet Friend wrote:
> ubytes get converted to ints when being added...
It's a common feature involving all integral type in languages like C,
C++, and D:
https://dlang.org/spec/type.html#integer-promotions
> On the topic of complaining about casting... array lengths as ulongs is
> painful... any chance of an array[].lengthi being a thing?
That is a topic sometimes with hot debate without any resolution.
Chances of it being changed in D is zero. :) Luckily, it's pretty easy
in :D ;)
long lengthi(T)(T[] arr) {
import std.exception : enforce;
import std.conv : to;
// No overflow here because this comparison is performed in 'ulong'
enforce(arr.length.to!ulong < long.max);
return arr.length.to!long;
}
unittest {
import std.traits;
auto arr = [ 1, 2 ];
static assert(isSigned!(typeof(arr.lengthi)));
assert(arr.lengthi == 2);
}
void main() {
}
Ali
More information about the Digitalmars-d-learn
mailing list