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

switching on boolean should not require `else` prong if both `true` and `false` prongs are present #1768

Closed
tgschultz opened this Issue Nov 21, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@tgschultz
Copy link
Contributor

tgschultz commented Nov 21, 2018

pub fn init(out_stream: *Stream) Self
{
    return Self
    {
        .out_stream = switch(is_packed)
        {
            true => BitOutStream(endian, Stream.Error).init(out_stream),
            false => out_stream,
        },
    };
}
error: else prong required when switching on type 'bool'

@andrewrk andrewrk added this to the 0.4.0 milestone Nov 21, 2018

@andrewrk andrewrk added the bug label Nov 21, 2018

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Nov 21, 2018

workaround: use an if expression

@tgschultz

This comment has been minimized.

Copy link
Contributor Author

tgschultz commented Nov 21, 2018

Alternative: use else instead of false. The reason I went with a switch instead of an if here is that the latter is, in my opinion, slightly harder to read:

pub fn init(out_stream: *Stream) Self
{
    return Self
    {
        .out_stream = blk:
        {
            if(is_packed) break :blk BitOutStream(endian, Stream.Error).init(out_stream);
            break :blk out_stream;
        },
    };
}
@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Nov 21, 2018

Assuming you're ok with using 105 columns, here's how I'd write it:

pub fn init(out_stream: *Stream) Self {
    return Self{
        .out_stream = if (is_packed) BitOutStream(endian, Stream.Error).init(out_stream) else out_stream,
    };
}

or

pub fn init(out_stream: *Stream) Self {
    return Self{
        .out_stream = if (is_packed)
            BitOutStream(endian, Stream.Error).init(out_stream)
        else
            out_stream,
    };
}

zig fmt is a bit hostile to both of those, which I'll make a note to myself to potentially remedy.

Regardless, it makes sense to make switch just work with bool types. Thanks for the report.

@tgschultz

This comment has been minimized.

Copy link
Contributor Author

tgschultz commented Nov 21, 2018

I treat the 100 char limit as a pretty hard limit personally, otherwise I would have written it the first way, and I never use a newline in a single line if except within the condition expression. Anyways, it's a nice low-priority low-effort fix for someone to tackle eventually.

@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Jan 4, 2019

This was fixed in #1857 -- please close.

@Hejsil Hejsil closed this Jan 4, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.