-
Notifications
You must be signed in to change notification settings - Fork 18k
spec: type inference doesn't permit inferring interface type parameter from argument #50484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This code has a generic interface type, and uses that type as a function parameter. You are suggesting that type inference should infer the type argument for the generic interface type based on the type being used to call the function. There is no type inference that supports that at present. In general we are being very cautious about type inference. It's essential that nobody ever be surprised by an inferred type. This isn't going to change for 1.18. We can consider this kind of type inference for a later release. |
The easiest workaround for this is to add a Make function, then inference will work correctly, like this: https://gotipplay.golang.org/p/kD0BP_bfQDa `package main import ( // This playground uses a development build of Go: type Quacker[Q any] interface { type Duck[Q any] struct { func MakeDuckQuacker[Q any](sound Q) Quacker[Q] { func (d Duck[Q]) Quack() { func ForceQuack[Q any](quacker Quacker[Q]) { func main() { |
unfortunately, this workaround doesn't solve practical cases where objects implement more than one interface. |
isn't that the whole point of having interfaces? if an object implements an interface, it should be usable wherever that interface is accepted
i'm really looking forward to that. this is one of the 3 key blockers to having proper generic collections in go. |
Yes, but as far as I can tell that's not what this issue is about. This issue is about whether we should infer the type argument for a parameterized interface type. It's not about the interface type, it's about inferring the type arguments. You can always write the type arguments explicitly yourself. |
This is a duplicate of #41176 AFAICS, so closing as such. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
great success
What did you see instead?
type Duck[string] of duck does not match CanQuack[Q] (cannot infer Q)
The text was updated successfully, but these errors were encountered: