Closed
Description
What version of Go are you using (go version
)?
$ go version go version go1.19 linux/amd64
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 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/mrfox131/.cache/go-build" GOENV="/home/mrfox131/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/mrfox131/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/mrfox131/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/lib/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4107873395=/tmp/go-build -gno-record-gcc-switches"
What did you do?
https://go.dev/play/p/e4HeQf10djz
What did you expect to see?
According to https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#pointer-method-example
I expected to see that constraint type inference will work for function type definitions too/
What did you see instead?
Constraint type inference does not work for type definitions where type is some function/
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
MrFox131 commentedon Aug 16, 2022
Title format was not really clear for me, so I apologize for the inappropriate title.
[-]affected: Generics feature[/-][+]cmd/compile: constraint type inference for type definition fails[/+]MrFox131 commentedon Aug 17, 2022
To be honest struct was not meant in docs, but I found out, that type inference for structs does not work too
https://go.dev/play/p/5JcVfw2zxxN
ianlancetaylor commentedon Aug 17, 2022
In Go 1.19 constraint type inference only applies to functions, not types. See https://go.dev/cl/404875. We can leave this issue open as a request to try to make this work in the future.
CC @griesemer @findleyr
MrFox131 commentedon Aug 17, 2022
Thanks for the reply. I think if this is implemented it will be much more logical than type inference only for functions. Most likely I will try to do this myself, I am already reading the guideline.
griesemer commentedon Aug 17, 2022
It turns out that type inference for types is much more difficult than type inference for functions. A function is not a type and thus cannot be used to instantiate another function or type (nor does it need to be inferred). But a type may be used to instantiate another type, and possibly itself. On top of that, if we combine this with type inference, there are all kinds of cycle problems that we currently can't handle correctly (and don't fully understand), and which may require significant (re-)engineering of the type checker. And it's not even clear that it's desirable as it may make code really difficult to read.
[-]cmd/compile: constraint type inference for type definition fails[/-][+]cmd/compile: constraint type inference for generic types is not implemented[/+]griesemer commentedon May 16, 2023
We don't plan to do this. Closing.