Skip to content

reflect: reflect.Type does not implement comparable #54064

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
oakad opened this issue Jul 26, 2022 · 3 comments
Closed

reflect: reflect.Type does not implement comparable #54064

oakad opened this issue Jul 26, 2022 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@oakad
Copy link

oakad commented Jul 26, 2022

Indeed:

func P[K comparable](d K) {
	fmt.Println(d)
}

func main() {
	d := reflect.TypeOf(int64(0))
	P(d)
}

I believe the above should work per the:

Type values are comparable, such as with the == operator, so they can be used as map keys. Two Type values are equal if they represent identical types.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 26, 2022
@cherrymui
Copy link
Member

cherrymui commented Jul 26, 2022

There are a few proposals about making comparable works better with interface types, including reflect.Type, see e.g. #51338 , #52509 , #53734 . Closing this as a dup. Thanks.

@cherrymui cherrymui closed this as not planned Won't fix, can't repro, duplicate, stale Jul 26, 2022
@oakad
Copy link
Author

oakad commented Jul 26, 2022

@cherrymui This must be at least mentioned in the docs. Because the word "comparable" now has specific meaning and the documentation for Type becomes misleading.

@randall77
Copy link
Contributor

The docs for the constraint comparable in the spec https://go.dev/ref/spec#Type_constraints are clear:

The predeclared interface type comparable denotes the set of all non-interface types that are comparable

reflect.Type is an interface type, and thus not comparable.

Note that this is different from the language meaning of "comparable", which is https://go.dev/ref/spec#Comparison_operators

I agree that using the same name for both is kind of confusing. Unfortunately English has limited words in this space so we have to make do with what we have. As the links that Cherry mentioned make clear, it was originally intended that comparable include interfaces, but we took it out because we were not sure about the semantics, and we wanted to be on the safe side of backwards compatibility.

@golang golang locked and limited conversation to collaborators Jul 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants