-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
reflect: add Value.Equal, Value.Comparable #46746
Comments
One very minor observation:
I'm not sure this is quite right. |
I think the statement is still true, because if the dynamic types of |
Ah, indeed. Thanks. The relevant sentence from the spec is:
|
We have:
So it sounds like we want to add the second one of these:
And maybe:
Comparable seems like a better name than CanEqual here, but Equal seems better than Compare (compare bytes.Compare, bytes.Equal). We probably want CanConvert at least for Go 1.17. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. |
If a (this is just a clarifying question, not a comment against the proposal) |
Is this still true? The window for doing this is pretty small now. |
Assuming you have a |
Change https://golang.org/cl/334669 mentions this issue: |
I sent https://golang.org/cl/334669 in case we do want this in 1.17. |
No change in consensus, so accepted. 🎉 |
For #395 For #46746 Change-Id: I4bfc094cf1cecd27ce48e31f92384cf470f371a6 Reviewed-on: https://go-review.googlesource.com/c/go/+/334669 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
In that case, I think the documentation is confusing — it explicitly states that it reports (emphasis mine) “whether the type of (The second sentence does state that “ |
Change https://go.dev/cl/435277 mentions this issue: |
For #46746 Change-Id: Ic7a31ddf7cd6bf6dd0db6b9eb3fee68fc180f72e Reviewed-on: https://go-review.googlesource.com/c/go/+/435277 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
We definitely explicitly chose above to call CanEqual Comparable instead (because it matches the language spec term). If CanConvert returns false and you call Convert anyway, that panics. In any other package panicking would probably not be the answer, but panicking is how reflect signals these kinds of conditions. It would be odd not to panic in this one function. |
This proposal has been added to the active column of the proposals project |
Sounds like this function should panic. Moving to likely accept for that decision. |
Based on the discussion above, this proposal seems like a likely accept. |
Are |
Change https://go.dev/cl/440037 mentions this issue: |
@go101 we already decided the names. This was reopened for the panic behavior. |
No change in consensus, so accepted. 🎉 |
OK. It is just that |
Assuming the two values are valid and non-comparable, Equal should panic. x := reflect.ValueOf([]int{1, 2, 3}) x.Equal(x) // can not report false, should panic Assuming one of them is non-comparable and the other is invalid, it should always report false. x := reflect.ValueOf([]int{1, 2, 3}) y := reflect.ValueOf(nil) x.Equal(y) // should report false For #46746. Change-Id: Ifecd77ca0b3de3019fae2be39048f9277831676c Reviewed-on: https://go-review.googlesource.com/c/go/+/440037 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Assuming the two values are valid and non-comparable, Equal should panic. x := reflect.ValueOf([]int{1, 2, 3}) x.Equal(x) // can not report false, should panic Assuming one of them is non-comparable and the other is invalid, it should always report false. x := reflect.ValueOf([]int{1, 2, 3}) y := reflect.ValueOf(nil) x.Equal(y) // should report false For golang#46746. Change-Id: Ifecd77ca0b3de3019fae2be39048f9277831676c Reviewed-on: https://go-review.googlesource.com/c/go/+/440037 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Change https://go.dev/cl/447798 mentions this issue: |
Change https://go.dev/cl/447798 mentions this issue: |
Everything here has been implemented. |
For #46746 Change-Id: I75ddb9ce24cd3394186562dae156fef9fe2d55d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/447798 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
In Go 1.17 we have introduced a conversion that can panic (#395). This is the first case in which a conversion can panic. This means that code that calls
reflect.Type.ConvertibleTo
and then, if that returnstrue
, callsreflect.Value.Convert
, can see an unexpected panic. (See #46730.)Separately, for a long time now it has been possible for a comparison to panic, when comparing two interface values that have the same dynamic type but for which the dynamic type is not comparable. Therefore, for a long time code that calls
reflect.Type.Comparable
and then, if that returns true, uses the==
operator can see an unexpected panic. (This is a fairly uncommon case as the problem only arises when working with indirectly accessed interface types, such as pointers to interfaces.)I propose adding two new methods to
reflect.Value
.The text was updated successfully, but these errors were encountered: