Skip to content
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

Unable to use wide functions to satisfy narrow interface #51062

Closed
JDeuce opened this issue Feb 8, 2022 · 1 comment
Closed

Unable to use wide functions to satisfy narrow interface #51062

JDeuce opened this issue Feb 8, 2022 · 1 comment

Comments

@JDeuce
Copy link

JDeuce commented Feb 8, 2022

What version of Go are you using (go version)?

Latest playground

$ go version

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

import "fmt"

type S interface {
	Resolve(string)
}

type impl struct{}

func (i *impl) Resolve(a interface{}) {
	fmt.Println(a)
}

func main() {
	fmt.Println("start")
	x := S(&impl{})
	x.Resolve("hi")
}

What did you expect to see?

Since the Resolve method in impl accepts interface{} it should be valid to pass it a string and it should be a valid implementation of the S interface

What did you see instead?

cannot convert &impl{} (type *impl) to type S:
	*impl does not implement S (wrong type for Resolve method)
		have Resolve(interface {})
		want Resolve(string)
@randall77
Copy link
Contributor

Method signatures in Go must match exactly for a type to satisfy an interface. Having arguments that are assignable to / return values that are assignable from is not enough.

Closing as a dup of #30602, there is much more discussion there.

@JDeuce JDeuce changed the title Unable to use more generic function to satisfy interface Unable to use wide functions to satisfy narrow interface Feb 8, 2022
@golang golang locked and limited conversation to collaborators Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants