Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I agree, I could not write it better myself.

Going down one level and finding a better way to express the sugar around `map` and `flatmap` on Monads (and then `withFilter` in Scala) without resorting to compiler magic would be really cool and would let programmers add their own constructs that operate on a lot more than monads.

Out of curiosity, do you know of another language that has something on the level of Result type in its standard library? I haven't seen one. I know Scala has Either/Try, however those are both inferior to Result as a potential Exception replacement.



Is Result not just a specialised Either? In what way is Either inferior?


> 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.


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: