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
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
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
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
@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
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