Skip to content
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

retry-loop question #174

Open
sivukhin opened this issue Jan 23, 2024 · 0 comments
Open

retry-loop question #174

sivukhin opened this issue Jan 23, 2024 · 0 comments

Comments

@sivukhin
Copy link

Read article about retry loop and had a thought:

@matklad, what do you think about following retry loop structure (unfortunatelly, Zig ranges can't countdown in opposite direction - so need to use while loop instead of for (5..0)):

fn retry_loop() !void {
    var i: i32 = 5;
    while (i >= 0) : (i -= 1) {
        switch (try action()) {
            .ok => break,
            .retry => |err| if (i == 0) return err else sleep(),
        }
    }
}

Formally, it breaks 3rd property about obvious upper bound (as it doesn't use for (0..5) construction) - but actually upper bound is still pretty obvious.

But this approach a bit less redundant and a bit more clear - we are making N attempts, not N + 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant