News for hackers. Stuff that matters.
Re:Errors are values by chrismorgan on Monday January 12, 2015 @09:48PM (#8877825)
What in Go is a return type of (Anything, error) with typically exactly one of the two being nil is in Rust the Result type, `enum Result<T, E> { Ok(T), Err(E) }`, something which is explicitly exclusively-either an OK T or an error E.

The equivalent of `x, err = expr; if err != nil { return err }` is very common in Rust; it would be written `x = match expr { Ok(x) => x, Err(e) => return Err(e) }`. It’s such a common pattern that a macro named try! was placed in the standard library for this incantation. Therefore you can write `x = try!(expr);` (or if you’re going to ignore the Ok variant, just `try!(expr)`).

Rust also warns you if you do nothing with a Result object; you can ignore the warning or suppress it, but it all makes it very much harder to ignore such error conditions. Go is one step up from C, where integral error-indicating return codes are the norm and are typically ignored, but I feel it’s still a fair way off the confidence that Rust gives me that I haven’t accidentally forgotten to deal with an error case. If you want people to get the message “Whatever you do, always check your errors!”, it helps if you design the language to make it hard for the user to forget.

(You people that know Rust, I’m ignoring the `FromError` part for simplicity. That’s only in libstd’s try!, anyway; libcore’s try! doesn’t have it.)

Re:Errors are values by espadrine on Tuesday January 13, 2015 @03:36AM (#8878703)
As an aside, the Go solution provided in the post has the following curious property.

If you have to call `write()` 10k times (say, in a loop), and get an error on the first write, instead of either unwinding (in C++) or returning (in Rust), that Go solution requires your program to check for nil 99,999 times.

Re:Errors are values by chrismorgan on Tuesday January 13, 2015 @07:58AM (#8879461)
I’d also estimate that of all the `write` calls the first is by a considerable margin the most likely to fail.
Re:Errors are values by dbaupp on Tuesday January 13, 2015 @05:11AM (#8878907)
To be fair, the Rust approach is mostly the same as the Go one on the machine, just harder to screw-up: the discriminant of the enum has to be checked to be able to extract the value (or extract the error) which is similar to checking `err` for `nil`.
Re:Errors are values by rwcarlsen on Monday January 12, 2015 @09:59PM (#8877867)
In Go I not infrequently make use of a non-nil value AND a non-nil error. A canonical example of this is the io.Reader interface in Go's standard library [1]. I think it is a very useful idiom particularly when dealing with things where failure is more normal - e.g. dealing with network services, disk IO, decoding partially corrupt exif data from images. Often you want a best-effort at the value even if you run into problems.

[1] http://golang.org/pkg/io/#Reader

Re:Errors are values by pcwalton on Monday January 12, 2015 @10:31PM (#8877967)
That can be done by either returning a tuple or providing a way to get a partial value out of your error type. (Result is generic over the error type, so you can add whatever extra data you'd like to your errors without forcing your callers to downcast.)
Re:Errors are values by masklinn on Tuesday January 13, 2015 @04:07AM (#8878768)
> In Go I not infrequently make use of a non-nil value AND a non-nil error.

The number of APIs making use of that pattern is very much infrequent, and doesn't justify it for the 99.99% of functions which don't.

> I think it is a very useful idiom particularly when dealing with things where failure is more normal

And you could have the exact same information by returning, say, `Either (Int, Error) Int`. Or even `Either (Int, Error) ()` since partial writes are apparently considered error cases.

Although I would really use a separate data type e.g.

    data WriteResult = Complete | Partial Int Cause | Error Cause
and let the application decide whether a partial write is an error or normal behaviour.
Re:Errors are values by sjustinas on Tuesday January 13, 2015 @03:37AM (#8878704)
Rust team is aware of this and working on a solution[1] as a part of the "IO reform".

[1] https://github.com/aturon/rfcs/blob/io-reader-writer/text/05...

Re:Errors are values by tel on Monday January 12, 2015 @09:51PM (#8877832)
(And with HKTs you can abstract this whole blog post into a fully generic one-liner applicable to basically any notion of error! I can't wait til Rust picks those up.)
Re:Errors are values by tpaksoy on Tuesday January 13, 2015 @07:09AM (#8879257)
Does Rust's (future) HKT implementation have an RFC or a PR available?
Re:Errors are values by steveklabnik on Tuesday January 13, 2015 @08:29AM (#8879613)
It does not. It's entirely speculation at this point, though many, many of us want to have it. Everything has been so focused on getting 1.0 out the door, and HKT should be backwards compatible. Special pains have been taken to consider HKT when designing APIs.
Re:Errors are values by rgbrenner on Monday January 12, 2015 @10:39PM (#8877995)
C, where integral error-indicating return codes are the norm and are typically ignored

I can assure you that error codes in C are not typically ignored. What kind of sloppy C code are you writing?

Re:Errors are values by wtf_is_up on Tuesday January 13, 2015 @12:54AM (#8878390)
How can you possibly offer that assurance?
Re:Errors are values by frou_dh on Monday January 12, 2015 @11:03PM (#8878074)
Using printf?
Re:Errors are values by bkeroack on Monday January 12, 2015 @11:08PM (#8878087)
Nor are they ignored in idiomatic Go.
Re:Errors are values by NateDad on Tuesday January 13, 2015 @08:33AM (#8879634)
It is very easy to produce linters for Go to find things like unchecked errors, for example https://github.com/kisielk/errcheck