Re:Generics can make your Go code slower
by mcronce on Wednesday March 30, 2022 @02:11PM
(#30858745)
What, specifically, do you mean when you say Rust is "awful to work with"? With C and C++ I agree, but I've had a drastically better development experience in Rust than Go.
[ Parent
| on HN ]
Re:Generics can make your Go code slower
by svnpenn on Wednesday March 30, 2022 @02:34PM
(#30859046)
You should probably read the rest of the comment...
Re:Generics can make your Go code slower
by mcronce on Wednesday March 30, 2022 @03:21PM
(#30859687)
Are Result and Option really the only thing? Because nesting scopes based on Err/None is rarely the right choice, just like nesting a scope based on `if err == nil` isn't typically something you want to do in Go, or `if errno == 0` in C
- You can panic trivially with `.unwrap()`
- You can propagate the condition up trivially with `?`
- `?` doesn't work with all types, but it does work with Option, and it does work with the vast majority of error types - making your custom error work with it is very easy (if you're whipping up an application or prototype and want your error handling very simple, `anyhow::Error` works great here)
- You can convert None to an Err condition trivially with `.ok_or()?`
- In cases where it makes sense, you can trivially use a default value with `.unwrap_or_default()`
And all of these use require a _lot_ less code than `if err != nil { return nil, err }`And all of these allow you to use the Ok/Some value directly in a function call, or in a method chain, while still enabling the compiler to force you to handle the Err/None case
The common theme here being "trivial" :) Result/Option are a big piece of that better developer experience.
Re:Generics can make your Go code slower
by rastignack on Thursday March 31, 2022 @03:22AM
(#30864702)
However in typical rust code bases this systematic wrapping causes a lot of unhandled panics.
Reading it feels just wrong.