Hacker News new | ask | show | jobs
by jazzypants 16 days ago
> - go style err!=nill style exception handling

Is this not widely considered to be the ugliest part of the language? Why do you prefer having to do this manually?

4 comments

As someone who is primarily a Go dev, I never understood this criticism and have never seen a language do it any better (but I'm open to suggestions). I think try/catch is worse. I kinda like JS's promise syntax with the `.then()` and `.catch()`, but it doesn't appear that different to Go's approach.
Returning Result/Options that the compiler forces you to check so you can't forget.

Common in ML-family languages and Rust. They've been brought into most mainstream languages like Java by now because of how useful they are.

You don't necessarily need a result/option type. Go could add a dependent typing facility that let you express a relationship between the first and subsequent return values.

Very few languages do that; an option type is simpler to implement in many respects. But in the case of Go, especially at this juncture, it's possible the simplest route is a dependent type, at least a specialized one. You don't need to introduce a fully generic dependent typing facilty.

Even simpler would just be type qualifers, e.g. optional or nilable, on the first return value, though that doesn't help with multivalue returns, where a simple dependent typing facility could fully express success vs error return semantics.

You could also do it with contracts, though more obliquely. One of the criticisms with C++ contracts was that a contract made it easier to optimize a routine for the declared relationships, but without absolutely mandating enforcement that the callee or caller also obeyed the contracts, which is more difficult to do. That meant there was the potential for contracts to result in more bugs caused by interactions between undefined behavior and the optimizer.

Odin has @(require_results) that forces you to do something with returned values (e.g. T, Maybe(T), (T, bool))

Where Maybe is a polymorphic union of T and nil but the core library returns either (T, bool) or (T, E) where E is an error enum.

Ahhhh, got it: enforced correctness. Fair enough, I can definitely see the benefit of that. I do wish Go had more ways to do that, like first-class enum support, exhaustive switch cases, and, yeah, forced error handling. golangci-lint does a lot of this heavy lifting on behalf of the Go compiler, but it would be nice to have the compiler enforce it.
I hope that doesn't mean the compiler inherits the extra work

I'm considering glojure as an interesting new hobby language to try because it's benchmarks are impressive

If the go compiler ends up bloating itself with checks its static analyser could/should have done it would be a shame

I mean that I run an external CLI tool called golangci-lint (a linter), and it does some things that the compiler arguably should do instead.
Personally I think Go style is worse, because the runtime and compiler don't force you to handle the error. I just had to fix a critical issue because a dev forgot to check the result coming from a function, exceptions would have forced it to happen.
> I kinda like JS's promise syntax with the `.then()` and `.catch()`, but it doesn't appear that different to Go's approach.

These days async/await is first-class and the function-based callback form of promises is largely discouraged. And for, at least in part, the same reason: it's harder to enforce correctness.

It is, which is why the or_return[1] keyword was created.

[1]: https://odin-lang.org/docs/overview/#or_return-operator

Errors as values instead of some special language construct is generally a nice thing (IMO)
Exceptions are values. There's nothing different between:

    T someFn() throws E;
    fn someFn() -> Result<T, E>;
    func someFn() (T, E)
In only one of those examples do you get a T that's invalid.
Some patterns, notably when a reasonable default can be provided, are better handled in Go. In such a case the error handling can often be omitted entirely. Neither Rust or Go recognize this pattern explicitly, however. In Go, as your comment, the function signature doesn't say the result value is actually a valid default when there is an error.
True, Go does require you to follow a convention here (dont use T if err is not nil). In practice it works pretty well, though obviously not bullet proof.

I believe some exceptional languages are similar, like Ruby. If you rescue but dont abort, the variable on the left hand side gets a nil when that may otherwise not be possible. Of course, that is handled cleanly as its own error.

I don't see how. Unless it's the Go example where you get (nil, err)?
yes
yeah that sucks, one of the many reasons I don't like Go error handling.
Yes it is and it's frankly odd we are still designing languages this way. It's a mess and a Result with typed errors is much more ergonomic and should be the standard. See either in scala for more.
I see your Either types and raise you a checked exception. People might have soured on the idea due to the way Java specifically implements the idea, but they can absolutely be done properly. They're also more ergonomic than an Either type, so that's nice.

I guess I have to justify my last statement, so here goes. We need to first establish the fact that checked exceptions and an Either type used in error handling are isomorphic constructs. Both are part of your type system (there is no difference between a function returning an `Either<T, E>` and a function returning a `T` or throwing an exception `E`), although one could correctly argue that an Either type is more widely applicable. Okay, so error handling ergonomics. The main thing about exceptions is that they separate out your happy path code from error handling. This is often a physical separation via having separate blocks with a try and a catch. You can of course have this kind of separation with an Either type, but at least in my experience the ergonomics are such that it's often nicer to just have the error boundary close to the fallible expression. Either that, or you have to start doing stack unwinding until you find an appropriate place for error handling -- except unlike with exceptions, this unwinding is done by hand.

Another thing that most exception systems give you are post-operations, usually in the form of some sort of a `finally` type dealio. And yeah, you can absolutely do the same thing with Either-based error handling, but it's not nearly as ergonomic as a finally-block.

And if people get annoyed at these being called exceptions, because they're not always exceptional, then we could call them something like "error conditions" or whatever floats one's banana, that's just bikeshedding.

The place where Java went wrong with its checked exceptions was two-fold:

a) you can't parameterise properly over the exceptions you get thrown b) whether an exception is checked or not is a property of its place in the class hierarchy instead of being separate from it

A large part of the first one is due to generics being added after the fact to the language, trying to preserve backwards compatibility which also lead to the kind of type erasure etc. you get in Java. And the second one is mostly just a mistake in the design of the exception hierarchy. Sadly that's now impossible to fix in Java specially so we're stuck with it, but other languages could still learn from it.