-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
"errorset" is confusing #1668
Comments
I'd once proposed simple error mechanism (#122), which was completely "local", i.e. it required no outside constructs like error sets, didn't pollute function signature and didn't require to predefine anything. I also proposed ability to disable language features which some people may find too complex: #1027. Current error system was one of suggested. |
zig error handling scheme is pretty simple imo. an error set is the set of all the error a function can return. take for example this:
the function what i dislike about zig error handling is that when you use the global error set and you mistype the name of an error, for example update: see here |
@emekoi: there are two places which need to care/know about the error:
Everything else is not needed. Declaration of error set is busywork with no real value; function signature with errors just makes them harder to read/change, it is also redundant (if compiler enforces handling of every error). Subsets/supersets and implicit casting takes the thing to C++ complexity level . Having one global error set for all errors per project is not practical, like making one huge C enum listing all possible error constants in the program. It becomes unmaintainable very quickly. |
This is incorrect. When no error set is specified -- as in The value of ErrorSets is that you can have the compiler error for you if you accidentally mis-type an error name (as in |
Well, any good C libraries specify an enum of all errors and documents a subset that each function can return. In Zig, we just have a feature for this, so we don't need to document these things, and get it wrong. |
@Hejsil: this feature is redundant. If the compiler makes sure that every possible error is handled, then there's no need to repeat that information within the code. Imagine doing this:
Now compare this with the current mechanism which uses several highly abstract concepts, including "implicit cast". So much complexity just to return and handle error. |
How would the compiler track indirect calls if the error set is not part of the function signature? For some code, these things don't matter, and you can use |
@Hejsil: Indirectly called functions either must not use errors (e.g by turning them into regular return value), or the compiler could do the whole program analysis. The first solution would be more desirable for me. Errors are/should be handy shortcuts, so that one does not need to create yet another useless helper type, when the dealing with that error is so trivial. If things get complicated, as with indirect calls, then errors are no longer desirable feature, but obstacle and should not be used. Platform dependent errors would handled by platform dependent code. I think (but could be mistaken) that this is implementable and intuitive to use. |
Any vtable/dynamic dispatch feature will not be able to use the error handling features of Zig then and
And they are. You don't have to declare any useless helpers for any trivial code that calls functions directly. Removing error sets will just make it so you have to write "useless" helpers for any indirect call, where you'd really wanna use the great error handling mechanism of Zig. If handeling errors is nonetrivial, then people will just ignore the errors. C is a good example of this.
And what about platform independent abstractions? Because of Zig's lazy analysis, we cannot infer all possible errors for all platforms. The best option for these abstractions is to define a superset for all platforms. |
@Hejsil: Maybe vtable mechanism could employ use some error annotation. This would be special case, ordinary functions should not need anything. It could be, that I am completely behind the times here - after 0.1.0 Zig added so many strange things, that I am often lost what still works and how. (This did happen to me before, with language Nim, after watching its development for few years. When I eventually dared to write something in it, I was overwhelmed. Nim is powerful, full of features, but incoherent and short on good docs.) |
@andrewrk since this issue relates to Go error handling... The Go community is in the midst of a heated debate about how, and whether, to enhance its minimalist (many would say simplistic) error handling. That discussion has surfaced a variety of ideas about error annotation and wrapping, and function- or package-scope error handlers. (For the past year, I've been the Go community's most active author re new features for error handling outside Google.) I think the Zig team might benefit from reviewing some of this material. Zig has some features that Go lacks, e.g. Thanks for your consideration, and kudos for undertaking an ambitious and necessary project! |
Since this is an issue about error handling you might consider just posting the links relevant to the issue to add to the discussion. With regards to Go's error handling, last I checked (which admittedly is a while ago, a Google search doesn't reveal any new info on that front) it doesn't have the capability to do exhaustiveness checks on anything, not even "Did you check at all?". There's value in not being overly dismissive, but I have a hard time seeing how an error handling system with that basis is one to take inspiration from. |
@GoNZooo I prefer not to debate Go error handling with folks who don't use the language. FYI, you missed the "Error Values" feature, landing in 1.13, and discussions around the Re links, I don't wish to hijack this thread. |
It seems reasonable to me that things germane to this discussion about error handling are brought up if they can contribute to the knowledge space so that people can make informed choices, yours truly included. |
All the planned ways to change how error sets work in the language are noted in other open issues. |
Creating this after discussion in #760 (comment).
I'm reading the documentation for Errors (https://ziglang.org/documentation/master/#Errors) and I think this feature is too convoluted. Maybe there's a way for most users to opt in to minimal functionality and error API surface, with a more expressive error API available if needed.
Please feel free to clarify my understanding, since I see Zig mostly from the outside, and not as a user. However, you hopefully will eventually have many such users, and it is important to make it accessible, usable, and readable. This is especially important to the API designs of third-party libraries which evolve out of the syntax and semantics of the language.
I'll give an example of a convention in Go that makes things clean and simple in error APIs. I know the most about Go, but I also know that exception handling can be really painful, and so can error chains in Rust, so I understand that type systems for error values are important to get right. I don't have that much familiarity with how other languages do this, or if there's a cleverer and better option out there.
Error APIs can make languages anywhere from very simple to very complicated to use. Consider a highly expressive template-metaprogrammed exception typesystem in C++ as the worst example. Please take care to be thoughtful about the design of error handling, since it could be a shining gold star on Zig's overall design.
Anyway, in Go there's a convention, which I really appreciate, to prioritize the design of the error API of a function type as follows. Each example shows why the complexity of API design and usage increases with each approach.
v := foo()
if v, err := foo(); err != nil { ... } else { ... }
if v, err := foo(); os.IsNotExist(err) { ... } else if err != nil { ... } else { ... }
error
interface and interact with: https://golang.org/pkg/os/exec/#ExitErrorThe text was updated successfully, but these errors were encountered: