Regarding pointers: For me, point number 2 (and 3) is the key that forces us having some way of expressing emptiness in a pointer. I would say that point 1 could be optimized by the compiler Regarding interfaces: Yep! nil-interfaces are a different beast. That's the reason I focused on just one nillable type (pointers) this time and, especially, left nil-interfaces away.
El lunes, 17 de febrero de 2020, 13:40:36 (UTC), Brian Candler escribió: > > A "usable" nil map, i.e. being able to insert into it straight away, I > sympathise with. Remember though that you can't append "in place" to a nil > slice: it returns a new slice object. > https://play.golang.org/p/dL-r74C5m_w > > I presume you don't want to write the same for maps as you do for slices: > > m2 := m.set(42, "hello") // instead of: m[42] = "hello" > > I suppose in principle it might be possible to pre-initialize all > variables and structs which contain a map to a dynamically-allocated > instance of that map type. > > However, I don't think the idea extends to pointers: > > 1. you are asking for all pointers to be pre-initialized to some > dynamically-generated object, which might need to be garbage-collected > immediately the pointer is changed to point to something else. That would > be very wasteful. > > 2. there's the problem with self-referential pointers, e.g. A contains *B > and B contains *A. > > 3. you lose the "nil-ness" property of pointers: the ability of pointers > to point to nothing at all (such as the tree example I gave). If you could > no longer point to nothing, how could you tell the difference between an > interior node and a leaf node of a tree? You would have to change all code > to use, say, a slice of pointers (of size 0 or 1) instead of a pointer. > > Finally, what would you do about nil interface variables? You don't even > know what *type* the value should have. > >> -- 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/b32bd59b-d3ed-41ff-a804-f36d5a0bf5b0%40googlegroups.com.
