Skip to content

Proposal: Multi-object if #8351

@Mouvedia

Description

@Mouvedia

In #7257, which has been accepted, @thejoshwolfe says:

The discussion of generalizing this idea to other syntactic constructs is interesting, but those ideas should be their own issue/proposal if they are going to be accepted.

status quo

const one: ?u32 = 1;
const two: ?u32 = 2;

// the second if is an expr hence it requires a ;
if (one) |foo| if (two) |bar| {
  // …
};

proposal

const one: ?u32 = 1;
const two: ?u32 = 2;

if (one, two) |foo, bar| {
  // …
}

It's pretty much straightforward to me, but Ill add that it is obviously a AND (not a OR) so you will reach the else if at least one of the two is null.
Of course the order will have to be matched for the else captures:

    const a: anyerror!u32 = 0;
    const b: anyerror!u32 = error.BadValue;

    if (a, b) |foo, bar| {
        unreachable;
    } else |_, err| {
        expect(err == error.BadValue);
    }

The only downsides that I see is if the if body is huge it will require a back and forth to check the order which can induce errors but on the other hand it will also promote the good practice of refactoring your code into smaller chunks (so that you see both the if and the else).

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions