--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAKbcuKjePVMPH1Fxv3tWAAm_msjAZKmtbJs2EMkQ7WGEgtAqyA%40mail.gmail.com.
Currently, if I understand correctly, the only way to do this is to declare a variable, and return that:
var zeroValue V
return zeroValue
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.
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:We'd then end up in the same position with strings, depending on how exactly the nil value of a string is defined:
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 := 3b := 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...@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.
Where confusion might arise is in the operations on nil. It's already weird that nil slices and zero-length slices are distinguishable:We'd then end up in the same position with strings, depending on how exactly the nil value of a string is defined:I don't think we *could* define it in that way. The zero value of a string must always be identical to the empty string, so as to not break existing code. That is, `nil` would become a way to write "", effectively.
I think if we'd a) made the predeclared identifier `nil` assignable to any type and b) expanded the special case for comparisons to all types, we'd get the desired effect without allowing arithmetic expressions like these.
Lastly, with all this in mind: I agree with Ian that overloading `nil` further is probably not good, even though we could. Most questions about it come in the form of "That function returns nil, but if I compare to nil it's false", or "I get a nil-pointer exception, but I checked for nil right before that" and those definitely wouldn't get any less confusing.
I wonder then why slice wasn't defined the same way - i.e. the zero value of a slice could have been "empty slice". Today, empty slice and nil slice are distinguishable - even if the empty slice has cap 0.I guess the problem is deciding whether it's cap(x) == 0 or len(x) == 0 which is the defining characteristic of the zero value.
I think if we'd a) made the predeclared identifier `nil` assignable to any type and b) expanded the special case for comparisons to all types, we'd get the desired effect without allowing arithmetic expressions like these.That would work. Hence:if v == nil { ... } // v has zero value, regardless of its typev = nil // reset to zero value of its typereturn nil // zero value returnLastly, with all this in mind: I agree with Ian that overloading `nil` further is probably not good, even though we could. Most questions about it come in the form of "That function returns nil, but if I compare to nil it's false", or "I get a nil-pointer exception, but I checked for nil right before that" and those definitely wouldn't get any less confusing.I believe the fundamental thing we're talking about here is the fact that an interface value can be nil, or it can contain a typed value, which for certain types may also happen to be nil. That issue isn't going away. The question is, does this proposal make it any worse?The only thing you can do with interface values, apart from calling methods on them, is to check their nilness. After doing Go for a while, I realised thatif abc == nil { ... }where "abc" is of an interface type, can only *possibly* be testing whether the interface contains a value or not. It could not possibly be testing the value inside the interface for nilness, because we have no idea what type that is: e.g. it could be a struct, or a pointer to a struct, or a named type based on int64, and not all of those are comparable to nil.Therefore, if all values became comparable to nil, then maybe people would have a stronger reason for expecting "abc == nil" to act on the value contained in the interface, rather than the interface itself.There have been various proposals to solve that. One is to have a different constant for nil interface values, but thenif err != nilinterface { ... }is fugly. You might as well introduce new syntax for testing nilness of interfaces only:if has(err) { ... }if err.(any) { ... }Or even make interfaces (only) usable directly in boolean contexts:if err { ... }It would be a sort of half-way type assertion: this interface contains some value, but I don't care what it is. It doesn't really solve the underlying issue, but since you've not done an explicit comparison with 'nil', maybe it's less surprising when you unwrap the value and find it contains 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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/aa57a605-512b-46e2-93da-2fbdfd5c4d41n%40googlegroups.com.
Why not allow nil to be used as the zero value for type variables, to fill this gap?return nil // == V(nil)