Skip to content

Generic type does not match when the type should match #68210

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

Closed
intangere opened this issue Jun 27, 2024 · 2 comments
Closed

Generic type does not match when the type should match #68210

intangere opened this issue Jun 27, 2024 · 2 comments

Comments

@intangere
Copy link

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

$ go version
go version go1.21.5 linux/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/phi/.cache/go-build'
GOENV='/home/phi/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/phi/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/phi/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3804273348=/tmp/go-build -gno-record-gcc-switches'
uname -sr: Linux 6.1.68-1-MANJARO
LSB Version:	n/a
Distributor ID:	ManjaroLinux
Description:	Manjaro Linux
Release:	23.1.0
Codename:	Vulcan
/usr/lib/libc.so.6: GNU C Library (GNU libc) stable release version 2.38.
gdb --version: GNU gdb (GDB) 13.2

What did you do?

package main

import "fmt"

func CallOneArg[T1 any](f func(T1), args []any) {
	f(args[0].(T1))
}

func CallTwoArgs[T1, T2 any](f func(T1, T2), args []any) {
	f(args[0].(T1), args[1].(T2))
}

func Caller[F any](f F, args []any) {
	switch len(args) {
		case 1:
			CallOneArg(f, args) // type F of f does not match func(T1) (cannot infer T1)
		case 2:
			CallTwoArgs(f, args) // type F of f does not match func(T1, T2) (cannot infer T1 and T2)
	}
}

func MyFunc1(arg1 string) {
	fmt.Println(arg1)
}

func MyFunc2(arg1 string, arg2 string) {
	fmt.Println(arg1, arg2)
}

func main() {
	Caller(MyFunc1, []any{"Hello World!"})
	Caller(MyFunc2, []any{"Hello World!", "Bye World!"})
}

What did you expect to see?

The program to compile because type F does match func(T1) in the first case, and func(T1, T2) in the second case.
If you explicitly type assert such as CallOneArg(any(f).(func(string)) and CallTwoArgs(any(f).(func(string, string)), args) it will compile, but that defeats the entire purpose of the code. I'm not sure if this is a bug or a case of type inference not being implemented but it makes sense for this to work.

What did you see instead?

./compile.go:16:15: type F of f does not match func(T1) (cannot infer T1)
./compile.go:18:16: type F of f does not match func(T1, T2) (cannot infer T1 and T2)

@seankhliao
Copy link
Member

I believe the compiler is correct, there is no set of type parameters you can explicitly pass to make the code compile as is.
F any is not a sufficient constraint for it to be used as a function, F could be any other value

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants