4 ms·
I disagree. Of the various approaches I've seen to cope with this problem, Rust's try! macro is the most elegant. Given the practical minded nature of Go and
by inconshreveable 12y ago
I disagree.
Of the various approaches I've seen to cope with this problem, Rust's try! macro is the most elegant.
Given the practical minded nature of Go and the core team's willingness to add in special cases to the language where appropriate, I think a Go version of try!() would be welcome and fit with the ethos of Go. It solves the 80% case and it doesn't mean the creation of a type-safe macro system.
The thesis of the blog post is that errors are values and thus you can program around the repetitive handling of them. But the proposed solution of writing a one-off implementation of Maybe like `errWriter` isn't an answer because:
1) Most importantly, when you need to call 3 functions which return differently typed values and can fail, there's nothing in the language to help you. This is the common case.
2) The implementation is now less obvious. To understand it, I need to read the `errWriter` helper code.
3) The code space you'll save by writing a new Maybe type is negligible.
- falcolas 12y agotry! isn't too hard to emulate in Go, simply write a wrapper that panic()'s on and error and passes the first value through. The rust Result idiom is easy to implement as well, and is used frequently in the database.sql package to handle nulls. In this way, rust and Go are not that different. The biggest difference is the lack of a generic Result, which means you have a NullString struct with a Valid and String method.
- nostrademons 12y ago> try! isn't too hard to emulate in Go, simply write a wrapper that panic()'s on and error and passes the first value through. Several such wrappers are present in the Go standard library, eg. template.Must: http://golang.org/pkg/text/template/#Must http://golang.org/pkg/text/template/#Must The thing is, without generics, you need to write a new wrapper for each type you're dealing. Add to that that this really isn't idiomatic Go style (code that panics all the time is frowned upon), and most people don't bother. This is a pretty good illustration of both the point in the article and why some folks don't like Go. It's programming with error values; you can decide exactly what you want to do with the error, including panicking when you encounter one, and use the language's own abstraction mechanisms to write that code. OTOH, the reason why we have exceptions in most mainstream languages is because many, many C programmers wrote the same error-handling code over and over again, and eventually someone decided that it ought to be a language feature. The point of a programming language is to abstract out common patterns that you'd otherwise have to write repetitively.
- Jweb_Guru 12y ago> try! isn't too hard to emulate in Go, simply write a wrapper that panic()'s on and error and passes the first value through. That isn't what try! does in Rust. You are thinking of unwrap().
- falcolas 12y agoYou're right. Thanks!