3 ms·
If I recall correctly we just couldn't come up with a great name for it. To me "expect" is a positive action, but the argument is about it failing to meet the e
by erickt 9y ago
If I recall correctly we just couldn't come up with a great name for it. To me "expect" is a positive action, but the argument is about it failing to meet the expectation. Semantically I like `thing.unwrap_or(|| panic!("failure message"))`. It feels more like what I would want to say, but it just is so wordy.
Ultimately I'm happy we just picked something and moved on, but still mildly annoys me whenever I write it. If only we found that perfect method name way back when...
- AlphaSite 9y agoThe java optional api uses orElseThrow which I think is quite clear.
- bluejekyll 9y agoAgreed, I've never liked 'expect' either; 'or_else_panic(msg)' would be much clearer Edit: 'or_panic(msg)' would be shorter and also good.
- erickt 9y agoI think that was one of the proposed variations, but we ended up picking the shorter .expect to cut down on repetition. We expected (ha) that this function would be used in these one-offs, so we wanted something more efficient.
- bluejekyll 9y agoAs an old Java hack, my allowance for repetition is high. Especially when using and IDE that basically writes the code for me ;)
- e12e 9y agootherwise(msg)?
- MaxGabriel 9y agoI would expect such a function to provide a default value
- e12e 9y agoI actually initially had it as sort of a callback: .otherwise(panic(msg)) (although I assume a rust panic! isn't really a function call). But isn't the way to get the default, simply to use unwrap()? In a simple script, failing to open a configuration file for reading is likely a show stopper, and you probably want to log/print an error (no such file, wrong permission, etc). But in, say, a paint program, you'd normally not want to panic and crash if the image file a user selected to open in a file dialog is invalid or went away between the click-to-select and the click-to-open. In such a program you'd want to handle most file errors much more defensively.
- AsyncAwait 9y agoIt is, but also quite long, which I do not think suits Rust naming conventions.
- bluejekyll 9y agoNot really though: 'unwrap_or_else' is of similar length.
- Double_Cast 9y ago.beware("could not open file");
- tomjakubowski 9y ago.be_wary_of("but hole")