-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 inferred to be empty still need to be unwrapped with try/catch #5226
Comments
I think that's the wanted use case. The function says it will probably return an error in future implementations, but cannot fail atm. So all invocations of |
I think that this is useful for future api compatibility. A library may have a function which may not error now, but may error in future versions. I've seen this pattern used often in go codebases where a function always returns a "nil" error. |
Yes this is intentional: without it you couldn't do a pattern like:
which is pretty common in zig (see e.g. streams). I don't know why you would want the inconsistency of the empty error set not needing |
I was going to make an issue that it doesn't complain when an function that says it can return an error doesn't return an error. In other words, this is allowed and compiles without error:
but I guess that is as intended due to the above mentioned use case of maybe returning an error in the future? |
@BarabasGitHub, that was the old behaviour, but was recently changed: #4564 |
The catch clause doesn't get compiled when the error set is empty, so you can safely do something like this as a workaround: pub fn wrapper(x: var) void {
impl(x) catch @compileError(@typeName(@TypeOf(x)) ++ " may cause an error, call impl instead.");
} That said, I agree with the OP that maybe this should work. Changing the error set of a function is a change to it's interface, and it's expected to have to update all call sites when a function's interface changes. So adding an error in the future isn't a valid reason to require catch now. The real question in my mind is whether That said, we have precedent for the opposite too. Expressions returning void are allowed to discard their value or put it into a variable. If Another option would be to redefine inferred error sets to say that if no error set is inferred it returns |
Example:
The text was updated successfully, but these errors were encountered: