3 ms·
Is Result not just a specialised Either? In what way is Either inferior?
by mhaymo 12y ago
Is Result not just a specialised Either? In what way is Either inferior?
- masklinn 12y ago> Is Result not just a specialised Either? In what way is Either inferior? It's better named when it comes to being a value/error union: Result/Ok/Err is somewhat more obvious than Either/Right/Left, especially for people who don't make the connection between "right" and "correct". Aside from that, Result was annotated with #[must_use], so the compiler will warn if you ignore a Result return value: fn main() { f1(); } fn f1() -> Result<(), ()> { Ok(()) } => > rustc test.rs test.rs:2:5: 2:10 warning: unused result which must be used, #[warn(unused_must_use)] on by default test.rs:2 f1(); ^~~~~ doing that with Either would be weird. Those are not huge, but they're small tweaks specially making Result a better fit for an exception replacement.
- benjaminjackman 12y agoI agree 100% that it is a specialized either. I would argue that either is inferior because programmers empirically won't be bothered remembering that Left = Err and Right = Ok. Also fixing your Left to conform to a common error type which supports chaining is directly is also pretty important. This can be accomplished in a few different ways, but it's going to be done so often that Either ends up never being used for anything other than Results base class / Result is implemented as a type-class on either, with crappy names for error and ok. Overally though I dislike Either, because to me, it is a symptom of a language lacking a way to declare anonymous type disjunctions inline[1], just like things like std::pair indicates a language lacks tuples. 1: Something like def parseToken : ParseError | Int | String looking at that maybe with inline type disjunctions the need for Result itself melts away.