Go compiler - where and how the implicit interface "comparable" used?

191 views
Skip to first unread message

messi...@gmail.com

unread,
Mar 11, 2021, 10:42:07 PM3/11/21
to golang-nuts
Hi, 

I'm reading the new typechecker source code of go compiler(under directory cmd/compile/internal/types2), during the initialization process, universe.go registers an implicit interface "comparable" inside function defPredeclaredComparable, I know it's supposed to be used for type comparison but failed to find where and how this interface and related method "==" is used. 

In predicates.go there's a function 'Comparable" to check whether a type is comparable or not, but seems there's nothing to do with the interface.

And I don't understand the lookup logic for method "==" in type.go neither, how could an Interface type have method named "=="?

I searched a lot from the code base but failed to find the answer, could anybody kindly give me some clue?

Thanks a lot :-)

messi...@gmail.com

unread,
Mar 11, 2021, 10:55:05 PM3/11/21
to golang-nuts
Does it use for generics constraints only? As described at https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#comparable-types-in-constraints . If so the type comparison and this interface is two unrelated things?

Axel Wagner

unread,
Mar 12, 2021, 2:03:00 AM3/12/21
to messi...@gmail.com, golang-nuts
On Fri, Mar 12, 2021 at 4:55 AM messi...@gmail.com <messi...@gmail.com> wrote:
Does it use for generics constraints only?

That is my understanding.
 
As described at https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#comparable-types-in-constraints . If so the type comparison and this interface is two unrelated things?

They are related in the sense that `comparable` expresses exactly the constraint of being able to compare values of a type. But the compiler already knows how to compare values of the same type, if that type is comparable. And the `comparable` interface won't be usable as an actual type which can have a value (that would be useless - you need two values of the same concrete type to actually compare, but there is, in general, no guarantee that two values of an interface type are the same concrete type). So there doesn't have to be a direct connection between the two.
 

On Friday, March 12, 2021 at 11:42:07 AM UTC+8 messi...@gmail.com wrote:
Hi, 

I'm reading the new typechecker source code of go compiler(under directory cmd/compile/internal/types2), during the initialization process, universe.go registers an implicit interface "comparable" inside function defPredeclaredComparable, I know it's supposed to be used for type comparison but failed to find where and how this interface and related method "==" is used. 

In predicates.go there's a function 'Comparable" to check whether a type is comparable or not, but seems there's nothing to do with the interface.

And I don't understand the lookup logic for method "==" in type.go neither, how could an Interface type have method named "=="?

I searched a lot from the code base but failed to find the answer, could anybody kindly give me some clue?

Thanks a lot :-)

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/50661f0e-289e-43f3-a9d7-a42feb77f7fbn%40googlegroups.com.

messi...@gmail.com

unread,
Mar 14, 2021, 10:37:14 PM3/14/21
to golang-nuts
Thanks for your explanation, that's very helpful :-) 
Reply all
Reply to author
Forward
0 new messages