Decimal to Integer assignment warning #336
Replies: 1 comment 1 reply
-
|
I spent some time looking into the AL compiler internals to better understand what's actually feasible here, and I wanted to share what I found and would love to hear what everyone thinks. What I foundIn the literal case: MyInteger := 3.14;the compiler silently folds The design trade-off
The core challenge is that correctness often depends on the runtime value, and that's something a static analyzer can't know. So there's a real trade-off between catching more issues and avoiding unnecessary warnings. To me, there isn't an obvious "right" answer, which is why I'd like to hear the community's perspective before we decide whether (and how) to implement this rule. I'd love to hear your thoughtsI'm curious how this shows up in real-world code, so if you've come across something similar, I'd really appreciate hearing about it. Whether you've run into this yourself, have an opinion on what a rule should (or shouldn't) flag, or can think of examples that illustrate the trade-offs, please share them. I'm especially interested in cases where the "right" behavior isn't obvious. Some things that might be useful to hear about are:
The more real-world examples we can collect here, the easier it will be to judge whether a rule makes sense at all, and if it does, where the right balance between accuracy and usefulness lies. Any feedback or real-world examples would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Today, I was reminded that where is no compile time check when assigning from decimal to integer type. The error only appears at runtime.
Please add warning when assigning decimal expression to integer without using Round(Number, Precission[, Direction]) with second parameter being integer expression. IMHO, if some custom function is doing the rounding and returns decimal it is still programmers responsibility to make sure the rounding result is actually an integer.
Actually, it is more complex than that. You could in theory do
IntegerVar := Round(3.14) * 100, orIntegerVar := 0.5 * 2or evenIntegerVar := Round(3.14) * Round(HundredsWithFraction, 1). Not sure if this should be allowed (almost certainly not - too complex analyzer for too little added value).Thanks.
P.S.: I have a question for the god - WHY? 😄

Beta Was this translation helpful? Give feedback.
All reactions