also, can someone please explain the issues around 'nil' ? i fail to appreciate author's concern about those...
also, can someone please explain the issues around 'nil' ? i fail to appreciate author's concern about those...
The question can be thought of as two parts:
1) Whether nil should be a base value for objects. If something points to type t, should that be allowed to be nil. It is obvious that it should if coming from C/C++/Python, not so obvious if coming from Haskell for example.
2) Should the lack of objects, or should error conditions be represented by nil. Say lack of a an object in a map is indicated by returning nil. Should it throw an an exception instead? Now, go doesn't have exceptions so it needs something like this perhaps. Or maybe panic should happen.
To expand on 1). If you are coming from Haskell (or any strong typed language) the thinking is, you are already paying the price for static checking, strong types, compilation step etc. It would be nice to have explicit non-nil types. You can say this function accepts a reference to this object and this cannot be nil. During compile time the typechecker/compiler will do a more rigorous analysis based on that, and barf out an error then instead of during production.
Like in Haskell you can explicitly say something may be nil (Nothing):
data Maybe a = Nothing | Just a
I don't personally lean one way or another here. I just don't have a strong opinion either way. I understand the trade-off. But this is how I interpret what the author means by his issue with 'nil'. Hopefully this helps. value, exists = myMap[42]However, null is just a special case of "invalid" pointer (e.g. point to freed memory), and it's very easy to create an invalid reference, e.g. take a reference into a vector and then push a pile more elements onto it, causing the vector to reallocate and move in memory, invalidating the reference.
yes, it does. thank you for your kind explanation.
Take it from the guy who _invented_ nil: http://qconlondon.com/london-2009/presentation/Null+Referenc...
[1]: http://blog.denevell.org/golang-null-nil.html struct foo bar () {
return NULL;
}
is a compilation error as well… (granted it's not an error for strings because C has no strings per-se) > (granted it's not an error for strings because C has no strings per-se)
That was kind of the entire point though.The difference is that Go has a value-type string and C does not, but that is only a parenthesised aside in your original comment (as an example of Go value types), nowhere near "the point" let alone "the entire point".