On Wednesday, 1 June 2022 at 02:19:43 UTC+1 Ian Lance Taylor wrote: > We could do that. The main concern is that "nil" is already > overloaded, and people new to Go are frequently confused by it. See > the FAQ entry https://go.dev/doc/faq#nil_error . Adding more uses of > nil will increase the potential for confusion. >
I think this is an interesting proposal. If "nil" were to mean "the zero value of *any* type" then you can argue it removes a layer of confusion. Not only is it the zero value for pointers, interfaces, slices, maps and channels, but also for strings, integers, structs, and anything else that comes along. Where confusion might arise is in the operations on nil. It's already weird that nil slices and zero-length slices are distinguishable: https://go.dev/play/p/6MVECg4onAk We'd then end up in the same position with strings, depending on how exactly the nil value of a string is defined: var s2 string if s2 == nil { ... } // but this is different to "" ? There would be an incentive for Go APIs to treat nil strings and empty strings differently (especially in SQL, JSON/YAML etc), and that would certainly be a bad thing IMO. And you might get some other strange stuff, like being able to do arithmetic on nil. a := 3 b := a + nil // b := a + 0 ?? var c float64 = nil + nil // ??? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7292a481-af17-49a3-b887-93ab55c35bcen%40googlegroups.com.