5 ms·
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)
by chrismorgan 12y ago
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.)
- tel 12y ago(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.)
- tpaksoy 12y agoDoes Rust's (future) HKT implementation have an RFC or a PR available?
- steveklabnik 12y agoIt 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.
- rwcarlsen 12y agoIn 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 http://golang.org/pkg/io/#Reader
- pcwalton 12y agoThat 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.)
- sjustinas 12y agoRust 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/0517-io-os-reform.md#nonatomic-results https://github.com/aturon/rfcs/blob/io-reader-writer/text/05...
- masklinn 12y ago> 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.
- rgbrenner 12y agoC, 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?
- espadrine 12y agoAs 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.
- dbaupp 12y agoTo 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`.
- chrismorgan 12y agoI’d also estimate that of all the `write` calls the first is by a considerable margin the most likely to fail.
- NateDad 12y agoIt is very easy to produce linters for Go to find things like unchecked errors, for example https://github.com/kisielk/errcheck https://github.com/kisielk/errcheck