News for hackers. Stuff that matters.
Re:Errors are values by modernserf on Monday January 12, 2015 @08:42PM (#8877643)
This looks suspiciously like implementing Maybe every time you need to deal with a series of errors.
Re:Errors are values by tel on Monday January 12, 2015 @08:47PM (#8877656)
It's exactly that (well, EitherT Error IO () perhaps) and the pattern would be far easier to replicate if Go had generics. So we're back here.

Edit: I wrote an elaboration, https://news.ycombinator.com/item?id=8877732

Re:Errors are values by inglor on Monday January 12, 2015 @08:59PM (#8877700)
Maybe and Either constantly show up when people discuss patterns to make error handling less clumsy in languages without exceptions baked in (and in asynchronous contexts in some languages with exceptions) - I just wish posts like this named what they're talking about better - Rob Pike is undoubtedly aware of the names.
Re:Errors are values by theseoafs on Monday January 12, 2015 @10:16PM (#8877917)
I would not be so sure Rob Pike knows about Maybe and Either. How would he?
Re:Errors are values by enneff on Tuesday January 13, 2015 @05:59AM (#8879044)
This is an amazing comment.
Re:Errors are values by NateDad on Tuesday January 13, 2015 @08:59AM (#8879832)
People are amazingly able to assume ignorance is the cause of disagreement.
Re:Errors are values by harryh on Tuesday January 13, 2015 @12:15PM (#8881121)
LOL LOL LOL. Very nice dig.

A+

Re:Errors are values by TheDong on Tuesday January 13, 2015 @02:16AM (#8878563)
Oh, I dunno, maybe the fact that he designed a programming language which practically proves he must have interest in programming languages. Combine that with the fact that Maybe/Options are neither new nor unusual in language design and fairly heavily discussed and I think it would be practically impossible for him to not have known about them a decade ago. Even failing at that, he would doubtlessly have heard of them since because people discuss them in relation to Go so often.

So yeah, that's how he would know about them. They're extremely common in two areas of his interest (Go discussion/criticisms, language design).

Re:Errors are values by theseoafs on Tuesday January 13, 2015 @01:04PM (#8881479)
Given that the party line on generics in Go has been "Go doesn't have generics, but since generics are mainly used to implement generic collections, you can just use the built-in collection types for those purposes", I wouldn't be shocked if the designers of the language only had a minimal or cursory understanding of Maybe, Either, and friends. I do not mean this as a dig or an insult, but if Rob Pike is at all familiar with the benefits of Maybe and Either (or sum types in general), then it does not show either in his work or in the talks he's given.
Re:Errors are values by prodigal_erik on Tuesday January 13, 2015 @11:23AM (#8880755)
Failing to solve a problem is not evidence that you are familiar with any solutions.
Re:Errors are values by spion on Monday January 12, 2015 @09:03PM (#8877705)
Unfortunately, its impossible to implement Maybe / Result / Either in Go. Doing that requires generics.
Re:Errors are values by falcolas on Monday January 12, 2015 @10:41PM (#8878004)
A generic maybe/result/either? Yes. However the maybe pattern is built into the NullString, NullFloat64, and NullInt64 types in the database/sql package.
Re:Errors are values by lmm on Tuesday January 13, 2015 @08:44AM (#8879711)
I guess that's the Go way, copy/paste the same code and change the types to be String, Float64, Int64, .... I'm not sure whether to laugh or cry.
Re:Errors are values by spion on Tuesday January 13, 2015 @03:27AM (#8878694)
That doesn't quite work, as the maybe pattern also implies a chaining operation (flatMap), which is impossible to implement without generics
Re:Errors are values by falcolas on Tuesday January 13, 2015 @09:37AM (#8880081)
You can still chain, you just have to do type conversion to and from the interface{} type.

It's not pretty (and not in broad adoption), but neither is it impossible.

Re:Errors are values by spion on Tuesday January 13, 2015 @10:38AM (#8880451)
That defeats the point of having a type system (casting is as error prone as forgetting to check errors).

Also it doesn't allow for the use of already defined functions to do the transformations, (e.g. `maybe.flatMap(existingFn).flatMap(otherFn)` wont ever work, new closures will have to be defined that do casts before calling the existing functions).

So basically, its just-as-error-prone and just-as-tedious as checking error codes but in a different way. Both benefits of the pattern are gone. Given this, I don't think that the word "impossible" is a stretch at all (not having any of the benefits of the pattern = not implementing the pattern)

Re:Errors are values by skybrian on Tuesday January 13, 2015 @01:14AM (#8878438)
Maybe is not an error-handling mechanism because it doesn't report any error message. I don't know why Haskell programmers think it's any good for reporting errors.

You could use Either, but it doesn't give you any guarantee that you'll get a string back, and the naming is lousy. (Left and Right? Really? I know there's a mnemomic, but we can do better than that.)

So, might as write your own ADT and name it right.

(Also, a real error-reporting mechanism should support cause chaining so you can see the dependencies; you can sort of do it with string concatenation but it gets awkward after a few levels.)

Re:Errors are values by masklinn on Tuesday January 13, 2015 @04:13AM (#8878784)
> So, might as write your own ADT and name it right.

http://doc.rust-lang.org/std/result/enum.Result.html

> (Also, a real error-reporting mechanism should support cause chaining so you can see the dependencies; you can sort of do it with string concatenation but it gets awkward after a few levels.)

http://doc.rust-lang.org/std/error/trait.Error.html

Either isn't custom-built for error reporting, so it makes sense that its naming only conventionally matches.

Re:Errors are values by codygman on Tuesday January 13, 2015 @01:45AM (#8878493)
> You could use Either, but it doesn't give you any guarantee that you'll get a string back

1. It's better not to use strings and instead use a user defined error type. 2. What do you mean "doesn't give you any guarantee you'll get a string back"?

> real error-reporting mechanism should support cause chaining so you can see the dependencies;

Can you elaborate on cause chaining, perhaps with an example?

Re:Errors are values by skybrian on Tuesday January 13, 2015 @04:06AM (#8878765)
What I mean is something like a stack trace except without all the extra detail:

  - Can't persist shopping cart
  - RPC request failed: http://example.com/update_cart
  - Can't write to database "carts"
  - Can't save file: "/a/b/c"
  - OS error: disk full
Ideally, all errors in the cause chain should be logged, and they typically cross abstraction boundaries; the message at each level comes from a different part of the stack. This seems hard to do across languages, but within a language there should be a standard way to do it.

(Go falls down on this too. Perhaps we don't need Java-style stack traces that go on for multiple screens, but we should have more than a string.)

Re:Errors are values by nbevans on Tuesday January 13, 2015 @01:59AM (#8878529)
Maybe is just an example. If you need to return more error codes, perhaps with contextual information such as message(s), then just define your own discriminated union and use that.
Re:Errors are values by thomasahle on Monday January 12, 2015 @08:48PM (#8877657)
Yep, I also think that's a good way to think about it. But I don't think implementing that one or two times for an entire program is too bad if it forces you to think error handling into your design.