Skip to content

proposal: reflect: reflect.TypeOf(nil).Kind() panics; let's make it return nil #54208

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
leozz37 opened this issue Aug 2, 2022 · 2 comments
Closed

Comments

@leozz37
Copy link

leozz37 commented Aug 2, 2022

The Kind() method of reflect.TypeOf() returns the type of a given value, so this snippet panics:

import "reflect"

func main() {
    reflect.TypeOf(nil).Kind()
}

panic: runtime error: invalid memory address or nil pointer dereference

When checking the type of a value, may occur that it can be nil, causing the program to crash. Iterating through a slice of interface and having a nil value can be a struggle.

There's many ways to solve this, but my propose is that reflect.TypeOf(nil).Kind() should returns nil.

I'm not sure this makes sense, but brought the topic to discuss.

@gopherbot gopherbot added this to the Proposal milestone Aug 2, 2022
@robpike
Copy link
Contributor

robpike commented Aug 2, 2022

Duplicate of #51649?

@ianlancetaylor
Copy link
Contributor

Yes, let's fold this into #51649. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2022
@golang golang locked and limited conversation to collaborators Aug 2, 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

4 participants