regarding what seems (to me) unnecessary casts on integer expressions

Kyle Ingraham kyle at kyleingraham.com
Sat Jun 5 01:34:06 UTC 2021


On Saturday, 5 June 2021 at 00:24:01 UTC, someone wrote:

On Saturday, 5 June 2021 at 00:24:01 UTC, someone wrote:
> ? 0 : cast(ushort)(this.pintBottom1 - 1); }

It looks like you’re being caught by D’s arithmetic conversions: 
https://dlang.org/spec/type.html#usual-arithmetic-conversions

“If the signed type is larger than the unsigned type, the 
unsigned type is converted to the signed type.”

Your pint variables are ushorts but the 1 you subtract is an int 
literal so the result gets promoted to an int.

Also, I don’t believe there is an unambiguous way to write a 
ushort literal: https://dlang.org/spec/type.html#basic-data-types

You should be ok with “u” if the function’s return is ushort. The 
compiler should be able to figure it out from there. I’m hoping 
I’m not wrong there as I haven’t tried it myself.


More information about the Digitalmars-d-learn mailing list