Skip to content

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

Closed
@andrewrk

Description

@andrewrk
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.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