Killing integer division problems?

burt invalid_email_address at cab.abc
Tue Aug 4 13:13:27 UTC 2020


On Tuesday, 4 August 2020 at 12:54:42 UTC, WebFreak001 wrote:
> On Tuesday, 4 August 2020 at 12:34:37 UTC, burt wrote:
>> [...]
>
> I agree having this as warning would be useful, however it 
> should not apply to:
>
> int x = y / z;
>
> but only to
>
> float x = y / z; (and double)

Yes exactly, I might not have been clear about this but that is 
what I intended.

> In my mind I would like if this was implemented something along 
> the lines "if this expression has an integer division in it and 
> is implicitly converted to float/double, emit a warning", if it 
> isn't implicitly converted to float/double it should not emit a 
> warning, as casts are quite ugly and may introduce bugs later 
> on if types change.
>
> Also alternative to casting, which would be safer:
>
> float x = int(y / z);

Sure.

> This is already valid syntax and is basically just like an 
> implicit assignment, just explicitly written out. This way you 
> can't accidentally change y or z to float/double without 
> triggering a compiler error.
>
> [...]
>
> Though I haven't really contributed to dmd, so I don't know how 
> feasible this actually is :)

Me neither... that's why I was asking around if it's feasible.


More information about the Digitalmars-d mailing list