> The reflect code panics, because you are comparing a struct value to nil. 
>> Struct values can not be nil.
>>
>> No. I’m comparing AN INTERFACE, not a struct.
>>
> You are using `reflect` to inspect the dynamic value of that interface. 
> That dynamic value is a struct.
>
No. It should be a POINTER to a struct, which would have been fine. Instead 
Go's reflection incorrectly latches on the type of the embedding structure.
 

> The interface’s provenance is _through_ a struct that has an embedded 
>> pointer, but this should not matter to the code that accepts the interface.
>>
> I completely agree with this. That's why it is so important not to store 
> invalid values in an interface. The code using that interface has no 
> reasonable way to check if the value is valid. So it has to rely on the 
> caller to only provide valid implementations.
>
The code is NOT using anything incorrect. The interface is obtained through 
a pointer to a struct, which is completely valid. The only problem is that 
this pointer is embedded inside a struct.

If you rewrite the code to use a named field, it would work. This means 
that the language behaves differently depending on a field having a name.
 
>
> But really, the solution here is not to "check better". It's to not store 
> invalid implementations of an interface in the interface. 
>
> I gave you the real example, somewhat simplified.
>
Then this is the real example with the fix applied: 
> https://go.dev/play/p/04H1KenHjRV
>
The real example would look like this:  https://go.dev/play/p/mwUfh4_RBUU - 
you do see why it's a tad problematic?

-- 
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/58d5770b-4888-4980-8f9e-c96310d57490n%40googlegroups.com.

Reply via email to