News for hackers. Stuff that matters.
Re:Why Go Gets Exceptions Right by eru on Friday September 14, 2012 @08:44AM (#4521323)
Do you have any opinion on using Monads for exception handling (like in Haskell)?
Re:Why Go Gets Exceptions Right by dllthomas on Friday September 14, 2012 @10:07AM (#4521673)
It's a way of getting context-specific (but not error-specific) behavior, and when dealing with functions producing a polymorphic monadic value it threads very nicely. That said, the approach in Haskell of fail always taking a string (which doesn't necessarily get stored/used anywhere) has a bit less to recommend it. It's also slightly confused conceptually to put "fail" in the Monad typeclass but that mostly bugs the theorists. The biggest downside, though, is that it can often make the source of errors harder to find - cf. hunting NaNs in floating point arithmetic.
Re:Why Go Gets Exceptions Right by eru on Friday September 14, 2012 @11:03AM (#4521961)
Oh, we just pretend that the monad typeclass does not have "fail" in it. It's only useful for pattern match failures when using the do-syntax.

I was talking more about the (Either Error) monad, which has not much to do with "fail".

(Edited first sentence.)

Re:Why Go Gets Exceptions Right by dllthomas on Friday September 14, 2012 @11:25AM (#4522112)
Ah, yeah, wasn't as aware of those as I should have been. Still suffers from the "where did that NaN come from?" issue, but looks like good stuff. Any idea why it's not used more?
Re:Why Go Gets Exceptions Right by eru on Friday September 14, 2012 @11:38AM (#4522193)
Haskell actually has three (or four) competing exception handling mechanisms nowadays.

If you use the Either error result approach, you can conceivable put a stack trace (or something similarly identifying) in the Error data-type, so you can avoid the "Where did that NaN come from?" issue.

I think we need to see Algebraic data types in more languages, before we will see this approach used more often. (In my opinion algebraic datatypes and the pattern matching they enable rank in the same league as garbage collection in that they are a feature originally invented in and for functional languages, but useful outside as well.)

Re:Why Go Gets Exceptions Right by dllthomas on Friday September 14, 2012 @11:49AM (#4522261)
> Haskell actually has three (or four) competing exception handling mechanisms nowadays.

Yeah, knew about Either in general (see my top-level comment about sums vs. products), just not the monadic bits around it (MonadError and ErrorT, in particular).

> If you use the Either error result approach, you can conceivable put a stack trace (or something similarly identifying) in the Error data-type, so you can avoid the "Where did that NaN come from?" issue.

True - even just making exceptions take a Loc parameter seems like it could be a good step, but that would be basically requiring TemplateHaskell, which would ruffle feathers for sure...

> I think we need to see Algebraic data types in more languages, before we will see this approach used more often. (In my opinion algebraic datatypes and the pattern matching they enable rank in the same league as garbage collection in that they are a feature originally invented in and for functional languages, but useful outside as well.)

I agree wholeheartedly. Lack of easy-to-use algebraic datatypes is my biggest pain-point in languages that lack 'em.

Re:Why Go Gets Exceptions Right by Lambdanaut on Friday September 14, 2012 @09:48AM (#4521588)
I like them. The only annoyance is that you're forced to wrap your return value in a monad even if the function succeeds. That's type safety for you!
Re:Why Go Gets Exceptions Right by eru on Friday September 14, 2012 @11:08AM (#4521992)
Yes. But that's because the default is non-monadic wrapped and an assumption that it can't go wrong. I'd had to see non-monadic wrapped values to silentry go wrong or raise an exception. (You can do something like Go's panic in pure values. But you are not supposed to do that for recoverable conditions.)
Re:Why Go Gets Exceptions Right by Evbn on Friday September 14, 2012 @09:23PM (#4524854)
In practice, wrapping a return value in a monad looks exactly like returning a regular value in another language: return x.