You could come up with `Either` types, but due to the lack of generic structs you'd have to have a whole lot of those.
Interestingly, Rust has basically the same error handling idiom, but verbosity is considerably reduced by the '?' operator (previously the try! macro).
When I was coding in Go, i would have loved that feature. The mind-numbing error checking in go is hugely annoying.
Ugh. Yes. I really want to love go. No, I really love go. But this is so annoying.
In the short term I have a really rudimentary library I've put up here: https://github.com/asteris-llc/gofpher and a presentation on it here https://github.com/rebeccaskinner/presentations/tree/master/... (should build with pdflatex, I need to get an actual pdf built soon)
Rob Pike has a blog post that looks at a similar, although ideologically different, approach to doing it: https://blog.golang.org/errors-are-values
I think the latter is more go-ish, although also less generic than the monadic approach- it's also more in keeping with the ideology of go, but in practice I still never see it being used much in production code.
if (err !== null) {
return callback(err);
}I now typically deal with this via a Must() function that takes to values and return the non-err value or panics if there was an err.
That means you can call it like con := Must(db.Connect(...)).(db.Connection)
I think as long as panics don't cross library boundaries you're fine. And panics can cross library functions if the function is called MustConnect(). (Which means it'd be neat to have a pre-processor that generated MustX from X if it wasn't already there...)
err := someOperation(args...)
if err != nil {
return err
}
that's 4 lines, 3 of them are error handlingThat's an odd thing to complain about.
if theErrorReturnedByThePreviousFunction != nil {
panic(theErrorReturnedByThePreviousFunction)
}(I am only half trolling).