Skip to content

Generics Type Inference #71789

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
carmichaeljr opened this issue Feb 17, 2025 · 2 comments
Closed

Generics Type Inference #71789

carmichaeljr opened this issue Feb 17, 2025 · 2 comments
Labels
LanguageProposal Issues describing a requested change to the Go language specification.

Comments

@carmichaeljr
Copy link

carmichaeljr commented Feb 17, 2025

Go version

go version go1.23.3 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/jack/.cache/go-build'
GOENV='/home/jack/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/jack/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jack/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.3'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jack/.config/go/telemetry'
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-build2450416570=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I have made the below example code:

// You can edit this code!
// Click here and start typing.
package main

type (
	IFace1[T any]            interface{ Method1(v T) bool }
	Impl[I IFace1[T], T any] struct{}

	Thingy[T any] struct{}
	Thingy2       struct{}
)

func (t Thingy[T]) Method1(v T) bool    { return false }
func (t Thingy2) Method1(v string) bool { return false }

func Func1[I IFace1[T], T any](v I)         {}
func Func2[I IFace1[T], T any](v IFace1[T]) {}

func main() {
	var tmp Impl[Thingy[int]]
	var tmp2 Impl[Thingy2]
	_ = tmp
	_ = tmp2

	Func1[Thingy[int]](Thingy[int]{})
	// Func1[Thingy[int]](Thingy2{}) // Error as expected
	Func1[Thingy2](Thingy2{})

	Func2[Thingy[int]](Thingy[int]{})
	// Func2[Thingy[int]](Thingy2{}) // Error as expected
	Func2[Thingy2](Thingy2{})
}

Go playground link: https://go.dev/play/p/0v8JtrzGqQn

What did you see happen?

A compiler error:

./prog.go:17:10: not enough type arguments for type Impl: have 1, want 2
./prog.go:18:11: not enough type arguments for type Impl: have 1, want 2

What did you expect to see?

I expected the second type parameter (T) to be able to be inferred for two reasons:

  • First reason: Method1 uses the T generic parameter in its signature. If an incorrect second type parameter is supplied the compiler complains about it, so it knows about this relation between the method signature and type parameter. For example, var tmp4 Impl[Thingy[int], string] results in:
./prog.go:21:16: Thingy[int] does not satisfy IFace1[string] (wrong type for method Method1)
		have Method1(int) bool
		want Method1(string) bool

Looking at the above example it appears like the user supplied second type parameter takes precedence over any value supplied in the first type parameter as the error expects the type that the user supplied in the second parameter. But if the second argument is not supplied, would it be safe to infer the type? If it is not an explanation as to why the type cannot be inferred or a counter example would be appreciated.

  • Second reason: Most surprisingly, functions do not have this limitation. The type inference works as I would expect on Func1 and Func2 in the example.
@gabyhelp gabyhelp added the LanguageProposal Issues describing a requested change to the Go language specification. label Feb 17, 2025
@seankhliao
Copy link
Member

https://go.dev/ref/spec#Type_inference

inference is only for functions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageProposal Issues describing a requested change to the Go language specification.
Projects
None yet
Development

No branches or pull requests

3 participants