Skip to content

overflow in arithmetic with <strike>Optionals</strike> errors #7821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RogierBrussee opened this issue Jan 18, 2021 · 2 comments
Closed

overflow in arithmetic with <strike>Optionals</strike> errors #7821

RogierBrussee opened this issue Jan 18, 2021 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@RogierBrussee
Copy link

RogierBrussee commented Jan 18, 2021

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.

@LemonBoy
Copy link
Contributor

Check out std.math

@RogierBrussee RogierBrussee changed the title overflow in arithmetic with Optionals overflow in arithmetic with <strike>Optionals</strike> errors Jan 20, 2021
@RogierBrussee
Copy link
Author

Ok so std.math defines

fn add(T: type a:T, b:T) error{Overflow}!T

then the proposal is to make that function the @-intrinsic (with or without "withOverflow" in the name), drop the existing @addWithOverflow and define operators +!, -! , *! and /! (where the latter failing with error{DivisionByZero}) so the syntax would be

const res = try a +! b ;

meaning

const res = try @add(a, b);

Luckily a +! b does not mean a + (!b) already, because ! is not defined on u and i integers :-).

At least for documentation purposes e.g. "ordinary" addition is defined s.t.

a + b == try a +! b orelse unreachable;

making +! /@add the more primitive operation (at least conceptually).

Note that (a +! b) *! c is a syntax error (if you are careful be careful and have just one potential failure per line),
but ( a + b) *! c is OK.

Indeed errors are probably nicer than optionals. Zig makes handling failure smooth, and overflow is always an option, so I think that making it as easy as possible to deal with overflow is good.

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jan 20, 2021
@Vexu Vexu added this to the 0.8.0 milestone Jan 26, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

5 participants