4 ms·
I can't speak for Rust, but in Go accessing properties of nil objects and dealing with 0-values for non-pointer values can cause bugs in certain contexts where
by tmpz22 7y ago
I can't speak for Rust, but in Go accessing properties of nil objects and dealing with 0-values for non-pointer values can cause bugs in certain contexts where you "let your guard down".
func getFoo() (*Foo, error)
func main(){
foo, err := getFoo()
if err != nil {
// be an adult and handle your error
}
foo.Work()
// panic because whoever implemented getFoo returned a nil object
}
Also worth noting dealing with booleans in JSON is wonky because boolean values default to false, even if that json property wasn't set over the wire. Thus you end up unmarshaling to a pointer to a boolean, which now requires nil guards throughout your codebase when working with that unmarshaled object...
- steveklabnik 7y agoRust does not have nil objects nor zero values. The equivalent rust code would fail to compile.
- erik_seaberg 7y agoGo also has https://golang.org/doc/faq#nil_error https://golang.org/doc/faq#nil_error, so interface != nil doesn't ensure it's safe to call any of its methods.