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

someone someone at somewhere.com
Sat Jun 5 01:46:45 UTC 2021


On Saturday, 5 June 2021 at 01:34:06 UTC, Kyle Ingraham wrote:

> 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.

As I am writing code today I am encountering a lot of these 
situations.

cast(ushort)(this.pintBottom1 - 1)

My first walkaround for this was intuitive:

this.pintBottom1 - cast(ushort) 1 /// I didn't know if eg: 1S was 
possible which should have been the proper way to handle it

... since pintBottom was already defined as ushort, but it didn't 
work also, so I ended with cast(ushort)(this.pintBottom1 - 1). I 
don't understand how to write typed code in D then. What's the 
point of declaring, for instance ushort's if then nothing will 
treat them as ushort's and I have to manually cast() everything 
to ushort() all the time ?


More information about the Digitalmars-d-learn mailing list