> Other people have said this too, but I want to emphasize that there are not 4 potential states of an interface value. There are 2 potential states: 1) interface does not hold any value, and is == nil; 2) interface holds a value, and is != nil.
Indeed, there are 2 states of interface observable from outside. Although one of these observable states gets expressed through 2 states invisible to outer world. Which isn't a problem on its own. > When you start talking about an interface as a fat pointer you are talking about how those 2 states are implemented. There are other possible implementations Yes. And that's not an issue on its own. The issue is, such implementation detail kind of "leaks" sometimes and bites you when you don't expect. And it does so because the way it gets constructed omits one specific detail of conversion from pointer to interface. On Thursday, August 27, 2020 at 9:58:21 PM UTC+3 Ian Lance Taylor wrote: > On Thu, Aug 27, 2020 at 3:53 AM targe...@gmail.com <targe...@gmail.com> > wrote: > > > > Please don't forget that interface pointer isn't simple. It's a > so-called "fat pointer" which consists of pointer to data and pointer to > methods table. Thus, we have 4 potential states - (nil, nil), (nil, > pointer), (pointer, nil) and (pointer,pointer). First one is just ordinary > nil interface. Last one is just ordinary interface pointer with all > operations callable without issue (as long as they're properly implemented, > of course). Third one with valid data pointer and nil table pointer can be > AFAIK created only through reflection and is safely considered "invalid > state". We're left with second state with nil data pointer and valid table > pointer, making interface methods callable on it. It wouldn't be a problem > - if such state couldn't be created by merely casting typed nil pointer > (typed pointer variable with nil value) to interface. Why is this an issue? > Because most interface implementations require valid data pointer. "Static" > (i.e. not needing data) implementations can be considered corner case. > > Other people have said this too, but I want to emphasize that there > are not 4 potential states of an interface value. There are 2 > potential states: 1) interface does not hold any value, and is == nil; > 2) interface holds a value, and is != nil. > > When you start talking about an interface as a fat pointer you are > talking about how those 2 states are implemented. There are other > possible implementations, and in fact the implementation in gccgo used > to be different. And even the implementation in the gc compiler has > changed; at one time ints and other small values were stored directly > in the interface value, but now we instead store a pointer to the > data. > > 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/5de71d2c-958b-47e5-8bd2-4e54b3d14150n%40googlegroups.com.