Hi,

I'm not a generics expert either, but I think there is a misunderstanding
here:

The A in the Wrap[A any] is not the same as the A in Val[A any]. When you
instantiate the Extract function with Extract[string], it expects a
Wrap[string], which is, in fact, just a type implementing isWrap. The Val[A
any] implements this method whatever the type it is instantiated with
(string, int...) so Extract[string](Val[int]{3}) is, from my understanding,
a completely valid expression. The only thing we can be certain of with
this code is that the type switch will **not** reach the default case only
precisely when the Val[A any] is instantiated with the same type as
Extract[A any] function.

Hope I'm not getting anything wrong :)

On Tue, Jun 21, 2022 at 12:27 AM Tsvi Benschar <tsvihbensc...@gmail.com>
wrote:

> 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
> <https://groups.google.com/d/msgid/golang-nuts/511571a9-044f-4a9b-8c5c-ed48c98206a5n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAMaoJV%3Du_hSaJFWzFPsrxit6x-LFsnL1aXw8z_hXYWrCZVY5bA%40mail.gmail.com.

Reply via email to