Provide runtime convertibility checks #110
Labels
⬇️ affects: code (interfaces)
Affects the way end users will interact with the library
📁 kind: enhancement
New feature or request
💪 effort: medium
Milestone
The overflow safety surface is pretty useful, but it's also just a heuristic. It can be too restrictive in some cases, and even perhaps too permissive in a few.
In practice, unit conversions should never happen in hot loops. Thus, it would be nice if every unit conversion could be checked at runtime. These checks can be very efficient. We can generate one at compile time for every conversion. For overflow risk, we can simply compare the actual runtime value to the (compile-time constant) threshold. And for truncation error, we can perform the mod operation.
Really, the only thing stopping us is: what do we do when the check fails? Different error handling strategies are appropriate for different domains. There is no "one true error handling strategy".
Fortunately, we can separate out two steps: there's the error handling, and then there's the checking as to whether it should trigger in the first place. For the latter, we can provide functions which simply return
bool
. Then each project can make their own "checked conversion" function that handles errors in their preferred way.The text was updated successfully, but these errors were encountered: