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
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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Lich's Laptop\AppData\Local\go-build
set GOENV=C:\Users\Lich's Laptop\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Lich's Laptop\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Lich's Laptop\go
set GOPRIVATE=
set GOPROXY=https://goproxy.io
set GOROOT=D:\Installed Software\- Dev -\Go 1.18.1
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=D:\Installed Software\- Dev -\Go 1.18.1\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.1
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\LICH'S~1\AppData\Local\Temp\go-build2554615698=/tmp/go-build -gno-record-gcc-switches
What did you do?
I'm trying to define a function that returns a generic type value,but without any parameter defined as the generic.
codes are something like below:
package main
import"encoding/json"typeObjectConstraintinterface {
Object|Object2Method()
}
funcDoSomething[ObjectWithConstraintObjectConstraint]() ObjectWithConstraint {
objectJson:="{\"content\":\"This Would Cause Compiling Error\" +}"varobjectObjectWithConstraintjson.Unmarshal([]byte(objectJson), object)
returnobject
}
typeObjectstruct {
contentstring
}
// Just for compiling,irrelevantfunc (Object) Method() {}
// Caused Compiling Error: cannot infer ObjectWithConstraint funcDoSomething_Wrapped() Object {
returnDoSomething()
}
typeObject2struct {
contentstring
}
// Just for compiling,irrelevantfunc (Object2) Method() {}
// Same stuff with type Object2funcDoSomething_Wrapped() Object2 {
returnDoSomething()
}
funcmain() {
object:=DoSomething()
object.Method()
}
It is able to compile with single type,since the compiler seems not gonna confuse with the type value that returned,
but with mutiple types defined in the generic constraint,things just gonna change.
As some sort of a fixture, add a dummy object in the parameter,it could just work:
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I'm trying to define a function that returns a generic type value,but without any parameter defined as the generic.
codes are something like below:
It is able to compile with single type,since the compiler seems not gonna confuse with the type value that returned,
but with mutiple types defined in the generic constraint,things just gonna change.
As some sort of a fixture, add a dummy object in the parameter,it could just work:
What did you expect to see?
it could just compile with the original code without that dummy,with some more accurate type inference
What did you see instead?
Compilation Error
The text was updated successfully, but these errors were encountered: