[go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
Recently I was trying to write a func using generics where I wanted to use a slice of an interface that would contain implementers of that interface and then pass those types to a generic function, but I ran into this error: type MyStruct of MySlice{} does not match inferred type MyInterface for

Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread 'Dan Kortschak' via golang-nuts
On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote: > Recently I was trying to write a func using generics where I wanted > to use a slice of an interface that would contain implementers of > that interface and then pass those types to a generic function, but I > ran into this error: > > type

Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
No, that pre-generics case is not sufficient for my use-case. For my use-case I need to be able to use other types besides []Suiter such as []int, e.g.: var n []int n = Append(n, 1) n = Append(n, 2) n = Append(n, 3) n = Append(n, 4) for _, i := range n { fmt.Printf("Int: %d\n", i) } See full

Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread 'Axel Wagner' via golang-nuts
This is purely a type-inference problem. If you explicitly instantiate `Append`, it works: https://goplay.tools/snippet/15RFAPFPl3y Type inference is still relatively limited. It might become possible to at some point omit the instantiation. I think this might be "Inferring based on interfaces" in

Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
@Axel Wagner — Thank you for adding clarity to this. Yes, it would be nice if we could get this to be handled with better type inference. But unless and until then, we have our workarounds. On Saturday, October 21, 2023 at 4:01:05 PM UTC-4 Axel Wagner wrote: > This is purely a type-inferenc

Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread tapi...@gmail.com
On Sunday, October 22, 2023 at 4:09:51 AM UTC+8 Mike Schinkel wrote: @Axel Wagner — Thank you for adding clarity to this. Yes, it would be nice if we could get this to be handled with better type inference. It is hard to call such type inference better. That is too aggressive. But unl