You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ go version
go version devel go1.18-051df0d722 Wed Sep 22 03:45:00 2021 +0000 windows/amd64
Does this issue reproduce with the latest release?
N/A
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=[...]
set GOENV=[...]
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=[...]
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=[...]
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=[...]
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=[...]
set GOVCS=
set GOVERSION=devel go1.18-051df0d722 Wed Sep 22 03:45:00 2021 +0000
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=[...]
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=[...]/tmp/go-build -gno-record-gcc-switches
What did you do?
package main
type MyType[T any] struct{}
func (m MyType[T]) F1(arg T) {}
func (m MyType[T]) F2() {
n := MyType[T]{}
m.F1(n)
}
What did you expect to see?
Successful compilation, since MyType[T] implements any.
What did you see instead?
incompatible type: cannot use n (variable of type MyType[T]) as T value
I'm not sure this is a bug, but I even if I read the design document, I don't really understand why I have this error. Even using https://golang.org/cl/350697 gave me no reason.
The text was updated successfully, but these errors were encountered:
This is not a bug. In method F2, the typechecker must be able to show that the the type of n (which is MyType[T]) is compatible with the required type of the argument of F1, which is T, for any possible value of T. This is not true generally, and I don't think there any value of T for which it is true specifically. To be concreate, consider T=int. MyType[int] is a defined type which is completely distinct from int, and so can not be used as an argument type where int is required.
Possibly your confusion is that the value of T, even though not constrained (because of the any), must be passed along the same when F2 calls F1. The value of T cannot change as you go from F2 to F1.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
N/A
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
Successful compilation, since
MyType[T]
implementsany
.What did you see instead?
I'm not sure this is a bug, but I even if I read the design document, I don't really understand why I have this error. Even using https://golang.org/cl/350697 gave me no reason.
The text was updated successfully, but these errors were encountered: