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

should the orelse operator be right-associative? #15108

Open
torque opened this issue Mar 29, 2023 · 3 comments
Open

should the orelse operator be right-associative? #15108

torque opened this issue Mar 29, 2023 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@torque
Copy link

torque commented Mar 29, 2023

Consider the following code:

pub fn main() void {
    const a: ?bool = false;
    const b: ?bool = null;
    _ = a orelse b orelse @panic("oh, no");
}

As of zig 0.11.0-dev.2298+5d63d1115, this fails to compile with the following error message:

example.zig:4:20: error: expected optional type, found 'bool'
_ = a orelse b orelse @panic("oh, no");
               ^~~~~~~~~~~~~~~~~~~~~~~

This error message indicates that the line is being parsed in a left-associative fashion, i.e.

_ = (a orelse b) orelse @compileError("should not reach this");

In my opinion, the more intuitive behavior would be for orelse to be right-associative, i.e.

_ = a orelse (b orelse @compileError("should not reach this"));

If this wasn't an explicit design choice, I think it makes sense to change it. Right-associativity would make orelse behave like a short-circuiting boolean operator, which I think is logical behavior given its naming and functionality. For directly applicable prior-art, both C#'s and Swift's null-coalescing operators are right-associative.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Mar 30, 2023
@Vexu Vexu added this to the 0.12.0 milestone Mar 30, 2023
@hryx
Copy link
Sponsor Contributor

hryx commented Apr 2, 2023

Note: possible interaction with the accepted proposal at #114 (comment) in which associativity is a factor (and which was written before orelse existed).

@N00byEdge
Copy link
Sponsor Contributor

N00byEdge commented May 1, 2024

I don't get how associativity plays into this...
if (a orelse b) orelse @panic()
here a is nonnull, b is never evaluated. The left hand side is nonnull so the panic isn't evaluated either.
if a orelse (b orelse @panic())
a is again nonnull and the right hand side is never evaluated.
This looks more like a compiler bug to me.

@nektro
Copy link
Contributor

nektro commented May 1, 2024

orelse isnt eager in its evaluation atm even for consts. that's why a orelse unreachable and a.? have different behavior for comptime-known optionals.

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

6 participants