Prefer Signed or Unsigned in D?

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 2 02:47:13 PDT 2015


On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
> C/C++ discussion here....
>
>    http://blog.robertelder.org/signed-or-unsigned-part-2/
>
> D rules here...
>
>    http://dlang.org/type.html#integer-promotions

It depends on the context.

You should take care of blending signed and unsigned:
comparison error, a is > b but...
---
uint a = 1;
int b = -1;
assert(a < b); // does not throw
---

You should take care to the index type in a loop:
loop that doesn't run at all because of an infered unsigned 
index...
---
auto array = new int[](8);
for (auto i = array.length - 1; i > -1; i--)
     array[i] = 8;
assert(array[0] == 0); // does not throw
---


More information about the Digitalmars-d-learn mailing list