Skip to content
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

generics: Generic function not accepting nil as an argument #51729

Closed
alwindoss opened this issue Mar 16, 2022 · 9 comments
Closed

generics: Generic function not accepting nil as an argument #51729

alwindoss opened this issue Mar 16, 2022 · 9 comments

Comments

@alwindoss
Copy link
Contributor

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

$ go version
go version go1.18 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="/opt/gohome/bin"
GOCACHE="/home/alwin/.cache/go-build"
GOENV="/home/alwin/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/opt/gohome/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/opt/gohome"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/alwin/Sandbox/dev/tap/go.mod"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build174870830=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I ran this program. Code in Play

What did you expect to see?

For the code to run without errors

What did you see instead?

./main.go:10:20: cannot infer T (/home/alwin/Sandbox/dev/tap/main.go:14:13)

@alwindoss
Copy link
Contributor Author

Problem is the line that is passing nil as a parameter to the generic function pass

@alwindoss alwindoss changed the title affected/package: generics: Generic function not accepting nil as argument Mar 16, 2022
@alwindoss alwindoss changed the title generics: Generic function not accepting nil as argument generics: Generic function not accepting nil as an argument Mar 16, 2022
@seankhliao
Copy link
Member

working as intended, nil has no type.

@alwindoss
Copy link
Contributor Author

But if there is a function with the argument of type interface{} i can always pass a nil.
any in this case is interface{} so why can't i pass nil?

@alwindoss
Copy link
Contributor Author

I think this issue should not be closed without a discussion. The point i have raised about the ability to pass nil to an argument of type interface{} should be considered.
By not doing it perhaps we are introducing ambiguity in the way interface{} type works before generics vs after generics

@randall77
Copy link
Contributor

You can always provide the type parameter explicitly, e.g. pass[*int](nil). The problem is if you just write pass(nil), what should T be? *int? []byte? It is underspecified.
If you want to pass a nil as an interface, use pass[any](nil).

@ianlancetaylor
Copy link
Member

@alwindoss There is a difference in using a variable of interface type and using a constraint of interface type. I wouldn't call it an ambiguity. A variable of interface type can be set to nil, meaning the zero value of the interface type. However, a constraint of interface type does not accept a type argument of nil. That is because a type argument must be a type, and nil is not a type, and nil does not have a type.

@alwindoss
Copy link
Contributor Author

Thanks @randall77 for showing how to make it work and @ianlancetaylor for explaining the difference.

I modified the program and ran and it passes. It passes because now it has a type that satisfies the constraint

Modified Code

@alwindoss
Copy link
Contributor Author

Just a thought @ianlancetaylor
This is something very subtle. Would it make sense to have an example in this tutorial https://go.dev/doc/tutorial/generics

@ianlancetaylor
Copy link
Member

I thought about it but I think it would be too complicated to put into the tutorial. I don't think it's something that people new to Go are likely to run into.

@golang golang locked and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants