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

else value when switching on error set should have optional capture value which is subset #769

Closed
andrewrk opened this issue Feb 11, 2018 · 2 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) !void {
    if (symLink(allocator, existing_path, new_path)) {
        return;
    } else |err| switch (err) {
        error.PathAlreadyExists => {},
        else => return err, // TODO zig should know this set does not include PathAlreadyExists
    }
    ...
}

It should not be possible for atomicSymLink to return error.PathAlreadyExists.

Here's my proposal:

pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) !void {
    if (symLink(allocator, existing_path, new_path)) {
        return;
    } else |err| switch (err) {
        error.PathAlreadyExists => {},
        else => |subset| return subset,
    }
    ...
}

In this example, subset has exactly the same value as err, except that it has a different type. The type is @typeOf(err) except the error set does not have any of the errors covered by the switch prongs above.

If the type of subset would be error{} then the else is dead code.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Feb 11, 2018
@andrewrk andrewrk added this to the 0.2.0 milestone Feb 11, 2018
@andrewrk andrewrk modified the milestones: 0.2.0, 0.3.0 Feb 28, 2018
@andrewrk andrewrk added the accepted This proposal is planned. label May 14, 2018
@andrewrk
Copy link
Member Author

andrewrk commented Aug 7, 2018

Also you should be able to do it with other switch items:

                (await (async self.channel.loop.bsdWaitKev(
                    @intCast(usize, fd), posix.EVFILT_VNODE, posix.NOTE_WRITE,
                ) catch unreachable)) catch |err| switch (err) {
                    error.EventNotFound => unreachable,
                    error.ProcessNotFound => unreachable,
                    error.AccessDenied, error.SystemResources => {
                        // TODO https://github.com/ziglang/zig/issues/769
                        const casted_err = @errSetCast(error{AccessDenied,SystemResources}, err);
                    },
                };

@andrewrk
Copy link
Member Author

This now works for the else capture value. What's left to do before closing is supporting a capture value when there are multiple error cases specified (the example in #769 (comment))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. 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

1 participant