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

Error Sets #759

Merged
merged 31 commits into from Feb 9, 2018
Merged

Error Sets #759

merged 31 commits into from Feb 9, 2018

Conversation

andrewrk
Copy link
Member

@andrewrk andrewrk commented Feb 9, 2018

This pull request removes the % prefix operator for making error unions. Instead we have the ! binary operator which makes an error union out of an error set and a payload type.

The literal translation of %T to this new code is error!T.
However this would not take advantage of error sets. It's
recommended to generally have all your functions which return possible
errors to use error set inference, like this:

fn foo() !void {
}

Then you can return void, or any error, and the error set is inferred.

There's no more such thing as a top level error declaration, but you
can create explicit error sets like this:

const MyErrorSet = error {
    OutOfMemory,
    FileNotFound,
};

The expression error.Something is syntactic sugar for error{Something}.Something.

You're always allowed to implicitly cast an error from a subset to a superset. error is a superset of all error sets.

You can get the compiler to tell you the possible errors for an inferred error set like this:

foo() catch |err| switch (err) {};

It's now recommended to generally leave off the else when switching on an error code.

These sed commands should get you 90% through the transition that this change causes.

# replace %T with !T in function definitions
sed -i 's/\(\bfn .*) \)%\(.*{\)$/\1!\2/g' $(find . -name "*.zig")
# delete top level error declarations
sed -i 's/^error .*$//g' $(find . -name "*.zig")

When we have automatic documentation generation, it will now be able to list all the possible errors that a given function can return, along with each error code's doc comments.

Closes #632

to help track down accidentally linking against a library
@andrewrk andrewrk merged commit 5911962 into master Feb 9, 2018
@andrewrk andrewrk deleted the error-sets branch February 9, 2018 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error sets
1 participant