On Wed, May 4, 2022 at 12:53 PM 'Axel Wagner' via golang-nuts <golang-nuts@googlegroups.com> wrote: > > On Wed, May 4, 2022 at 9:46 PM Ian Lance Taylor <i...@golang.org> wrote: >> >> Zero sized values are useful in Go, because they can have methods and >> they can be stored in interfaces. But if the addresses of zero-sized >> values must be distinct, then although zero-sized values appear to >> have zero size they must in fact be implemented as taking up one byte. >> For example, given `struct { a, b struct{} }`, the addresses of `a` >> and `b` must be distinct, so that struct is actually two bytes. So, >> sure, we could change it. But there are surprising results either >> way. >> >> (Historically this was introduced for https://go.dev/issue/2620.) > > > The other side of the question is "why not define them to always be the > same"? It seems a fairly simple optimization. > I guess it can be argued that `a[0:0:0]` points to a zero-sized object, so > should always be the same, but that's expensive to do, as every slicing > operation has to check for 0? But that seems a weak argument. > Is there a better one?
Using the reflect package we can walk through the fields of a struct and get the address based on the size and alignment of earlier fields in the struct. But if the address of all zero-sized values is the same, then that doesn't work. It's not a great argument, but it's not nothing. Also it's a little weird that given `var v1 v2 struct { f1 int; f2 struct{}; f3 int }` then &v1.f2 == &v2.f2, but maybe that's not much weirder than how it works today. Ian -- 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/CAOyqgcXBQ14JMzAJbN9NZ%2BJeMYPRH9Za0G7iJDQgQ4eUGqLvBQ%40mail.gmail.com.