7 ms·
My biggest beef with the Go typesystem is that they didn't get rid of nil. Tony Hoare (the guy who invented null) has acknowledged they were his "billion dolla
by nostrademons 13y ago
My biggest beef with the Go typesystem is that they didn't get rid of nil. Tony Hoare (the guy who invented null) has acknowledged they were his "billion dollar mistake" [1], common practice in Java and Haskell is moving away from them, and yet Go included them anyway - in a language that's supposed to be robust because you're supposed to handle every case. The Maybe (or @Nullable) type is a much better idea, because it flips the default from "any variable may be null" to "only variables explicitly declared as such may be null".
[1] http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare http://www.infoq.com/presentations/Null-References-The-Billi...
- frou_dh 13y agoIt's exacerbated by nil being reused as a value for things that aren't even pointer types, like the slices, channels and especially interface "pairs"[1] [1] (<NoType> . nil) vs (T . nil) is pretty weird: http://play.golang.org/p/fmk-72OYkO http://play.golang.org/p/fmk-72OYkO
- shurcooL 13y agoThere is no such thing as a Go variable with no type. They all have a type (could be interface{}) and a value (could be nil). package main import ( "fmt" "github.com/davecgh/go-spew/spew" ) func main() { var x interface{} var y chan (int) spew.Dump(x) spew.Dump(y) var z interface{} = y spew.Dump(z) fmt.Println(x == z) fmt.Println(y == z) } // Output: (interface {}) <nil> (chan int) <nil> (chan int) <nil> false true
- frou_dh 13y agoDoesn't an interface value have both a static type and a dynamic type and in a case like x its dynamic type is "nothing"? I didn't mean that this is necessarily bad, just that it seems weird for "nil" to be overloaded beyond pointers.
- shurcooL 13y agoYeah. From the Go spec: "The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type also have a distinct dynamic type, which is the actual type of the value stored in the variable at run time. The dynamic type may vary during execution but is always assignable to the static type of the interface variable. For non-interface types, the dynamic type is always the static type."
- kkowalczyk 13y agoHoare didn't invent 0-valued pointer. It was there since the beginning of time. Or at least the beginning of the CPUs. People talk about getting rid of nil like it's actually possible. It's not. If you have pointers, they sometimes have to start the life uninitialized (i.e. with a value of 0) hence nil pointers. As you admit yourself, the proposed solutions don't actually get rid of anything. At best they can force you to handle nil value by wrapping it in some wrapper. Guess what - if you have that kind of discipline, you can do the same in C++. Why aren't people doing that? Because it comes at a cost. There's a cost to the wrapper and when it comes down to it, you still have to write the code to handle the nil pointer whether you're using a wrapper or a raw pointer. It just doesn't buy you much. Finally, fixing crashes caused by referencing a null pointer is downright trivial. Spend a few weeks chasing a memory corruption caused by multi-threading and you'll come to a conclusion that fixing a null pointer crashes (which can be done just by looking at the crashing callstack most of the time) is not such a big deal after all.
- JoshTriplett 13y ago> If you have pointers, they sometimes have to start the life uninitialized (i.e. with a value of 0) hence nil pointers. Right, and those pointers must then be declared as potentially null. Or more generally, potentially non-present values of any type need a type like Maybe T. > As you admit yourself, the proposed solutions don't actually get rid of anything. At best they can force you to handle nil value by wrapping it in some wrapper. That's exactly the point: you can tell from a glance at any type whether it can be null or not, and you can only look at the value of something that's guaranteed to not be null; otherwise, you have to handle the null case first. Forcing that to happen at compile time via the type system is far better than dereferencing a null pointer at runtime.
- eru 13y ago> If you have pointers, they sometimes have to start the life uninitialized (i.e. with a value of 0) hence nil pointers. Why do your variables have to start life uninitialized? > Why aren't people doing that? Because it comes at a cost. [...] Not a runtime cost, though. This can be handled purely by the typesystem at compile time.
- 13y ago
- AYBABTME 13y agoIn 97.5% of case, the `nil` value of Go types follows the NullObject pattern. There's really just nil pointers that are still equivalent to `null`, and their absence would be an abnormality, as there's no reason why a pointer couldn't have a nil representation.