-
Notifications
You must be signed in to change notification settings - Fork 13.3k
return Option instead of assert!/fail! #15535
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
Comments
cc @aturon |
fail! is bad if we can avoid by returning Option. 2014-07-09 5:51 GMT+08:00 Alex Crichton [email protected]:
by Liigo, http://blog.csdn.net/liigo/ |
I see failing here as analogous to failing in the slice indexing operator, (I guess if we were to add syntax a la RFC PR 13 for a slice operator, |
@pnkfelix you are right its kind of analogous. The problem with fail is you can't really react to it (like you could in other languages with exceptions) you have to prevent it so you need to put checks in front of every call you do just to let the function itself check it again.
I would not come up with that check and i certainly would not want to write this myself every time i call slice or any other method that calls slice internally (which are a lot). I don't think you should have to think I guess what i'm asking is a consistent way to handle errors through Option/Result instead of the current mix of some functions that return Option/Result and some you have to check the parameters before you do the call. For the indexing and the slicing operator failure might be ok because you would expect that coming from other languages and it would be tedious to writing |
@lucidd The title of your ticket implies a broad change that I assume would affect not only string slices, but also array/vector slices. But it sounds like your issue is really with string slices alone, in terms of needing to maintain the utf8 property. Is that a correct understanding? If so, is there a different proposal you can think of that would address your string handling issues while leaving array/vector slices alone? |
Array/vector slices have the same issue, just to a smaller extent. Adding an additional set of methods that return Option or Result instead of failing (like Haskell's safe package) seems like it would address this. |
I used StrSlice just as an example. Like @lucy said arrays/vectors have the similar problems and i don't think there should be a difference in error handling between them. It should not really matter what i want to slice/index or whatever the error handling should be the same. My main point is reducing the overall use of fail! and replace it with Options/Result to make it easier to react to errors. Functions on arrays/vectors and strings seemed to be a good way to start since they are commonly used and profit the most from easy error handling. |
I've been working on a set of guidelines for Rust, which includes a section on signaling errors. It's a quite tricky issue! A couple of thoughts beyond the guidelines: First, it is possible to recover from Second, cases like incorrect indexing are generally thought to be programming errors. In particular, you avoid them not by adding redundant checks before each operation, but instead by programming in a way that naturally gives you valid inputs. For example, a traditional One way to look at this is: if you did get an More generally, it's not a goal for all kinds of errors to be treated in the same way. A file not found error is quite a different beast from out of memory or index out of bounds. The guidelines linked above lay out some general classification of errors, and the appropriate way to signal them. |
+1. the same i wanted to say. 2014-07-09 22:58 GMT+08:00 Kevin Walter [email protected]:
by Liigo, http://blog.csdn.net/liigo/ |
@aturon Thanks for the link. I know that you can catch fail on a tasks but i did not consider using a task just to catch a possible failure on a simple function to be a valid solution. Fail is currently used in a lot of places where other languages would throw exceptions. But fail is by no means an exception replacement if the only way to "catch" it is to use a separate task. Here are a few problems i see in the current description of when to use
This it true for a lot of functions that already return Option including Vec::pop. I can just use
There are of cause ways to get only valid inputs and not have to introduce redundant checks but
like i said before there are already functions that return None on a clearly logical error like I feel like the whole |
Avoiding (We have moved to using
Note that "safety" has a fairly specific meaning in Rust: memory safety. (Unfortunately, Rust can't solve every problem.) |
I disagree with you. Fail! is more horrible than returning Option, IMO.
|
@liigo let's not turn the issue tracker into a series of "you're wrong" "no you're wrong" comments These issues are not resolved by community votes. Instead, try to provide new technical insight or a fresh perspective; those are much better for core team members wrestling with these issues. |
See what Kevin Walter said that fail! is bad than options. |
@pnkfelix I didn't said "you are wrong", i just said "i disagree with you". |
@lucidd Regarding your point here:
Indeed, both the original As a follow-up to @aturon 's comment above (that I hope is consistent with the philosophy he is espousing): If there exist functions in the stdlib that can But I do not regard the examples in the description as instances of the above, since the If there exist functions where the check is trivial but the methods for doing such checks are not documented, then we should fix the documentation to say how to do the checks. As I said above, if we do add a slice operator syntax, then I would be much more amenable to making the non-operator methods return |
See also discussion on PR #15652 starting here; in particular, there are some opinions presented there that imply that we might remove |
If a condition can be easily and efficiently checked before calling the function, it makes sense to consider it a logic error. It makes sense for indexing (or slicing) to fail for arrays, but it's much less sensible for hash tables and trees because the only way to find out if it's going to work is to try an expensive operation. |
There are quite a few functions on traits for slices that fail when given out of bounds parameters rather than returning an Option.
There are probably a lot more functions that could change to Option rather than fail but those are the ones i found so far.
If this is something we would want to change, i would offer myself to work on that.
The text was updated successfully, but these errors were encountered: