On Monday, 12 July 2021 at 23:25:13 UTC, Ali Çehreli wrote:
On 7/12/21 3:35 PM, someone wrote:

>>> private size_t pintSequenceCurrent = cast(size_t) 0;
>
>> Style: There's no need for the casts (throughout).
>
> [...] besides, it won't hurt, and it helps me in many ways.

I think you are doing it only for literal values but in general, casts can be very cumbersome and harmful.

Cumbersome and harmful ... could you explain ?

For example, if we change the parameter from 'int' to 'long', the cast in the function body is a bug to be chased and fixed:

// Used to be 'int arg'
void foo(long arg) {
  // ...
  auto a = cast(int)arg;  // BUG?
  // ...
}

nope, I'll never do such a downcast UNLESS I previously tested with if () {} for proper int range; I use cast a lot, but this is mainly because I am used to strongly-typed languages etc etc, for example if for whatever reason I have to:

ushort a = 250;
ubyte b = cast(ubyte) a;

I'll do:

ushort a = 250;
ubyte b = cast(ubyte) 0; /// redundant of course; but we don't have nulls in D for ints so this is muscle-memory if (a <= 255) { /// or ubyte.max instead of 255 (I think it is possible)
   b = cast(ubyte) a;
}

void main() {
  foo(long.max);
}

Ali


Reply via email to