-
Notifications
You must be signed in to change notification settings - Fork 18k
spec: when do struct comparisons panic? #38676
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
Comments
I believe the relevant part of the spec is:
A very literal reading of the spec suggests Brad’s program should print “panic, panic”. Currently it prints “not-equal, panic”. The CL Brad linked to would not change the output for this particular program, but for similar programs, it might change the output to “not-equal, not-equal”. |
It would be unfortunate if, when comparing |
I think this is #8606 |
@mdempsky, thanks! Closing this one as a duplicate. |
If both of the filed names are changed to package main
import "fmt"
func cmp(a, b S) {
defer func() {
if recover() != nil {
fmt.Println("panic")
}
}()
if a == b {
fmt.Println("equal")
} else {
fmt.Println("not-equal")
}
}
type S struct{_, _ interface{} }
func main() {
var f func()
cmp(S{1, f}, S{2, f}) // equal
cmp(S{f, 1}, S{f, 2}) // equal
} |
https://golang.org/ref/spec#Comparison_operators: "Two struct values are equal if their corresponding non-blank fields are equal." |
Should the spec clarify when a struct comparison panics?
e.g.
https://play.golang.org/p/4NUu89RI27t
Is it okay for implementations to have different answers to those two prints?
(Background: https://go-review.googlesource.com/c/go/+/230207,
cmd/compile: improve generated eq algs for structs containing interfaces
from @josharian)/cc @griesemer @ianlancetaylor @mdempsky
The text was updated successfully, but these errors were encountered: