3 ms·
While it's too late to change the name "expect", could one create an alias for it and call it say, "on_error"?
by eriknstr 9y ago
While it's too late to change the name "expect", could one create an alias for it and call it say, "on_error"?
- eximius 9y agoI'd expect 'on_error' to take a function as a callback, not a string. But yes, a better named function could be added
- skybrian 9y agoPerhaps or_die, similar to Perl?
- sanderjd 9y agoYep, this is what I always wanted it to be! But maybe or_panic would be better. Naming things is hard.
- eternalban 9y agoexpect[ing_error]("could not open the file")
- rootlocus 9y agoYou're not expecting an error. On the contrary, you're expecting a value or throwing an error.
- eternalban 9y agoYou're expecting specific errors.
- allan_s 9y agoor_fail_with("") ?
- IshKebab 9y agoOr `unwrap_msg()`?
- dleslie 9y agoA failed expectation for one user isn't always an error for other users; calling out on_error may confuse people because it indicates an error may occur and must be handled.
- edmccard 9y agoIn my Rust crates, I use a custom trait[1], with implementations for Option and Result, that provides an expected() method which, if it panics, prefixes the message with "expected: ". So you can write foo().pop().expected("foo length >= 1"); and if it fails, the error is something like "panicked at 'expected foo length >= 1'". [1]https://gist.github.com/edmccard/8898dd397eec0ff3595c28ada5221405 https://gist.github.com/edmccard/8898dd397eec0ff3595c28ada52...