overflow in arithmetic with <strike>Optionals</strike> errors #7821
Labels
proposal
This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone
Uh oh!
There was an error while loading. Please reload this page.
Zig has both intrinsics like
fn @addWithOverflow(T : type, a:T, b:T, res: *T) bool
and optional types. While being explicit about the types of the summands seems useful, in a language that has optionals it seems natural to write
fn @addWithOverflow(T: type a:T, b:T) ?T
Better still, with the precedent of operators like +% it seems that operators +?, -? , *? and /? would fit in so one can write
if( a+? b) |res|{
//no overflow
} else {
// overflow
}
One documentation advantage is that a + b can be defined as a + b == (a +? b).? including the undefined behaviour.
The text was updated successfully, but these errors were encountered: