Thanks for the response, this makes sense wrt duck typing. > I don't understand what your code is trying to do...
The code I posted was a minimal working example. In my original use case I was trying to emulate a generic sum type, where the sum type was an interface with an unexported dummy method implemented by each of its cases. So I thought that eg a maybe type could look like this <https://go.dev/play/p/NNz_l0TzNoe>. As I understand it now, if I wanted a function with a parameter Maybe[int] to exclude an input of Some[string], I could do something like this <https://go.dev/play/p/AHJFd2GCyGx> instead. I'm now realizing that even that wouldn't fully behave as I wanted because it doesn't account for nil values, so I'll have to figure out a better solution. On Monday, June 20, 2022 at 6:49:10 PM UTC-4 Ian Lance Taylor wrote: > On Mon, Jun 20, 2022 at 3:27 PM Tsvi Benschar <tsvihb...@gmail.com> wrote: > > > > Hey everyone, reposting a question 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 that compiles. (Here's the correct > version.) 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? > > Thanks for providing a complete test case. > > The Wrap interface ignores its type parameter, so any type that > implements a isWrap method with no arguments and no results will > implement Wrap with any type argument. The argument to Extract is > Wrap[A], so you can pass anything with an isWrap method to Extract. > The type Val[int] does have an isWrap method, so it's fine to pass > that type to Extract[int]. > > I don't understand what your code is trying to do, but the behavior > seems entirely correct to me. > > 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/74dbf908-8c02-4e51-b73c-bede28bf1893n%40googlegroups.com.