Hey everyone, reposting a question 
<https://forum.golangbridge.org/t/generic-interface-compiles-with-incorrectly-instantiated-types/27809>
 I 
asked on the go forum since it isn't getting replies there. I have some 
code that’s compiling and running despite there being a type error. I don’t 
understand go generics and interfaces that well, so I don’t know if this 
behavior is intended.

Here's a simple incorrect program <https://go.dev/play/p/UpEffRk8oB9> that 
compiles. (Here's the correct version <https://go.dev/play/p/pE9AWb-xvda>.) 
On line 23 of the incorrect version, the function's generic parameter is 
instantiated to string, but its input is of type int. It compiles anyways 
and panics at runtime, even though the type switch's default case should 
never be reached.

I got some similar examples to compile and run, they all involve 
instantiating a generic interface with one type but using it as if it had a 
different type. Is this behavior intended, or maybe is there some implicit 
unsafe cast happening? Or is this a bug?

-- 
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/511571a9-044f-4a9b-8c5c-ed48c98206a5n%40googlegroups.com.

Reply via email to