-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: any
no longer implements comparable
#51257
Comments
CC @ianlancetaylor, @findleyr. |
Notably, if interfaces do not satisfy
|
CC @griesemer @rsc I agree with the conclusions of #50646 -- that we should opt for logical consistency and type safety -- but also agree that this may warrant more discussion. In particular, we should probably analyze whether this decision is reversible: would it be possible to relax these restrictions in the future? |
@findleyr If that conclusion remains, would an alternative be to allow If neither of those are allowed, is it the case that this kind of code is simply not allowed in Go? Or is there some workaround we're missing? |
Is there any reason to believe we can't relax this in a future release? |
I agree that we don't have to do anything now. I think the path forward is the suggestion above: permit But we aren't going to do that for the 1.18 release. |
FWIW I am not sure that
It is odd that |
Another hypothetical solution would be to add a constraint that expresses the set of all interface types. Then the constraint for "all types which have the |
I believe this is just a bug: When we check if a type implements type T struct{ x any }
func main() {
NewSetFromSlice([]T{
{"foo"},
{5},
})
} the type I think this needs to be fixed for 1.18 otherwise we'll have to maintain this inconsistency. I agree that the long-term solution is to allow
|
Change https://go.dev/cl/387055 mentions this issue: |
Btw I also noticed I couldn't do this: _ := Set[Set[string]]{} With the same error - Maybe that is a similar issue? As it seems that it's just a struct which contains entirely comparable fields. Hopefully that fix will work here too |
@billinghamj Note that
A |
I think this issue should have gone through the proposal process. The type parameters design doc explicitly states:
The definition of “comparable” at the time that proposal was approved states (emphasis mine):
By those definitions, every interface type should be included in the type-set of Redefining |
@bcmills I agree that under normal circumstances this should have gone through the proposal process. But we've been long past the time where we should be making design decisions and we have run into what seems a clear inconsistency (#50646). As a matter or "real software engineering" (borrowing somewhat freely from "realpolitik"), being overly conservative (by being consistent) allows us to relax rules down the road while remaining backward compatible - at least that was the intent. The good news is that changing behavior, at least as far as the type system implementation is concerned, is simple. But first we need to settle on what the "right" behavior is. Again, given the imminence of the release, being overly cautious seems to be the prudent approach, even if it comes at the cost of not being able to write certain (or even many) programs. |
Sorry for stumbling into this closed issue. This change may create code that depends on the behavior that |
Change https://go.dev/cl/401874 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/jML-W5X8dks?v=gotip
What did you expect to see?
(no error)
What did you see instead?
Alternatively, using
comparable
rather thanany
, this error:Ref: #50646 (comment)
I understand that this seems to be intended, but I think it's going to make it practically quite difficult for developers to use generics in Go - as far as I can tell, there is simply no way to make this work
I think this should be reconsidered, or at least discussed more widely - as it does not seem to be specified in the proposal
This seems significant enough to be considered as a release blocker
A library which is affected in practice: deckarep/golang-set#79 (using its
generics
branch) - I have been trying to find workarounds to allow for this new behaviour, but have not managed to resolve itThe text was updated successfully, but these errors were encountered: