6 ms·
Huge no to anything that will require .unwrap() noise everywhere in the codebase. I use Erlang, my code does not have ".unwrap().unwrap()"[1] anywhere. [1] htt
by johnisgood 5y ago
Huge no to anything that will require .unwrap() noise everywhere in the codebase. I use Erlang, my code does not have ".unwrap().unwrap()"[1] anywhere.
[1] https://github.com/SeaQL/sea-orm/blob/64c54f8ad603df0c1d9da85ce7b0d9b59dc32fe4/examples/rocket_example/src/main.rs#L56 https://github.com/SeaQL/sea-orm/blob/64c54f8ad603df0c1d9da8...
- imron 5y agoI use rust and my code does not have .unwrap() (and definitely not .unwrap().unwrap()) in it either. That's what the ? operator is for.
- eru 5y agoThe '?' is not a conceptual advance over .unwrap(), it's "just" a major syntactic convenience.
- ComputerGuru 5y agoOf course it is. Unwrap panics on error, while ? bubbles up the error instead. But ? wasn’t introduced to replace unwrap, unwrap was always frowned upon. It instead replaces match and return.
- imron 5y agoIt was always frowned upon, but still ended up everywhere (including lots of documentation and example code) because it was less verbose than the match handling. The ? helped remove that last obstacle.
- TheDong 5y agoThe comparison here is with go. Unwrap would look like: file := os.Open("foo").Unwrap() I'll take that over the current go state of the art: file, err := os.Open("foo") if err != nil { panic(err) } Just like "panic(err)" is used infrequently, "unwrap" would be used infrequently. They're comparable, and for the cases where unwrap is okay (test code, once-off scripts, etc), I'd definitely prefer it to the panic boilerplate.
- tptacek 5y agoI don't understand this example at all. A Rust programmer would essentially never use unwrap on the return from File::open. It would be like checking open(2) with assert() in C.
- preseinger 5y agoThat Go would never pass code review unless it is part of a tiny CLI tool or script.
- eru 5y agoThat's actually a problem in Erlang. In Haskell, 'Maybe (Maybe Int)' is a different type from 'Maybe Int'. That means that when you use eg a hash table that returns some kind null value like 'Nothing' on lookup when a key is not found, you can still stick exactly that kind of null value as a normal value into the table and everything will turn out fine.