-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
spec: document definition of comparable
#50791
Comments
What would it be an alias of? |
At the moment we have the equivalent of type comparable <magic_comparable_interface_literal> in the universe scope. As it is now, there is an underlying type, which is that magic interface which doesn't have a name and can't be printed correctly. I can't think of a way to make the distinction visible inside Go at the moment, but it's visible through the If we make Perhaps it should be a "basic" type like |
I'm confused about why making it an alias would change the underlying type, but perhaps my understanding that point that doesn't matter. Totally agree about it being its own underlying type. Calling it a 'basic' type would work too. |
Agree about it being its own underlying type, which means it can't be a defined type. I think making it a basic type (like unsafe.Pointer) could be problematic. We'd have to introduce a new Making |
Need to mention this in the spec in the section on underlying types, and need to adjust implementation accordingly. See conclusion. |
comparable
be an alias type (like any
)comparable
is its own underlying type
Change https://golang.org/cl/380754 mentions this issue: |
If we ever permit variables to have type I think that implies that |
The underlying type of Making |
FWIW, when I wrote it my comment I didn't see much advantage to having |
cc: @danscales because this will also effect the representation of |
I don't feel strongly about any of this, but I think we need to answer a couple of questions. If we ever permit variables of Why should I don't see why |
If we agree that the underlying type of Because |
We do have some precedent: |
Narrow question: for |
See #50791 (comment). It should always return itself. |
Regarding the behavior of |
I'm not sure why the underlying type of Since we can't represent the underlying type of Then, it seems to me we can either do As Ian points out, given that Of course, we do still have to have a method of printing and distinguishing C when reflect, etc. wants to look at it. I'm not sure that there is a good way other than a new basic kind. |
@danscales: see #50791 (comment) : What do we print for the underlying type of |
No, we wouldn't. The reflect package does not need to distinguish interfaces that are the result of embedding from interfaces that simply list the same sets of methods. The reflect package only describes the runtime behavior of types, not the way in which they were defined. For that matter, aliases are completely invisible in the reflect package. |
That's exactly my point: how would we express the behavior that the interface pointed to by x can only hold comparable types? type I interface { comparable }
var x *I |
Yes, I agree that we still have to have something to print out for C and possible a new kind or magic bit for C. (I had already edited my previous comment to say something like that, but should have made a new comment.) I think we could print out |
@findleyr Ah, sorry for misunderstanding. Yes, as I mentioned earlier, we would have to add some way to determine whether all comparable types implement that interface. As far as I can see this would have to be a new method on |
I guess my main concern about making |
Apologies, I missed that you had mentioned that explicitly in your second comment. I was still thinking about your original comment :) I think the fact that a new API would be required in any case makes it less important that the universe comparable type has a name. |
Change https://golang.org/cl/380854 mentions this issue: |
For #50791. Change-Id: I7f135bb6626128a3cee9fd71c57535c1fc83ac7f Reviewed-on: https://go-review.googlesource.com/c/go/+/380854 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This comment has been minimized.
This comment has been minimized.
Per further discussions with @ianlancetaylor and @findleyr: The updated spec already says that all predeclared types are named types. Thus, we can define it as: type comparable interface{ /* magically marked as comparable */ } which is what we already have in the implementation. When printing the underlying type of In short: The underlying type of |
comparable
is its own underlying typecomparable
Change https://golang.org/cl/381094 mentions this issue: |
Change https://golang.org/cl/381076 mentions this issue: |
…omparable}" For #50791. Change-Id: Ib12009d2895146e55ec3a51aa8ceafe58dfd82a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/380754 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Follow-up on CL 380754. For #50791. Change-Id: Ia2f8f9785c2f02647525e7ee4168991fd4066dd3 Reviewed-on: https://go-review.googlesource.com/c/go/+/381094 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This reverts CL 380854. Per the conluding discussions on #50791. A follow-up will document `comparable` more thoroughly. For #50791. Change-Id: I15db9051784a012f713e28d725c3b8bbfeb40569 Reviewed-on: https://go-review.googlesource.com/c/go/+/381076 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Just bike shedding but maybe interface {==} could be the magical one? |
Isn't it going to be easier if we have operator method? No magic anymore:
|
It could but why introduce a new notation for something we'll never need? We have the name
We've tried operator methods repeatedly over the course of developing generics. They have their own (numerous) problems. That ship has sailed (and sunk) a long long time ago. Except for documentation, I think this issue is resolved. Thanks. |
It would be an interesting story to hear about the failure due to the proposal doc have only two places mentioning operator method but none of them mentioned about the sunk of that approach and only says "unplanned". |
There's only so much time in one's life to write down every single path taken which ended in a dead end. Sometimes it's just two short mentions... :-) |
Change https://golang.org/cl/381896 mentions this issue: |
I think this should be reflected by the declaration of https://github.com/golang/go/blob/master/src/builtin/builtin.go#L102 -type comparable comparable
+type comparable interface{ comparable } |
For golang#50791. Change-Id: I7f135bb6626128a3cee9fd71c57535c1fc83ac7f Reviewed-on: https://go-review.googlesource.com/c/go/+/380854 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
…omparable}" For golang#50791. Change-Id: Ib12009d2895146e55ec3a51aa8ceafe58dfd82a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/380754 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Follow-up on CL 380754. For golang#50791. Change-Id: Ia2f8f9785c2f02647525e7ee4168991fd4066dd3 Reviewed-on: https://go-review.googlesource.com/c/go/+/381094 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This reverts CL 380854. Per the conluding discussions on golang#50791. A follow-up will document `comparable` more thoroughly. For golang#50791. Change-Id: I15db9051784a012f713e28d725c3b8bbfeb40569 Reviewed-on: https://go-review.googlesource.com/c/go/+/381076 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Title says it all. Reminder issue.
cc: @ianlancetaylor
cc: @findleyr
cc: @mdempsky
The text was updated successfully, but these errors were encountered: