25 ms·
It is very confusing that equality in JavaScript is not transitive: https://imgur.com/5pFXFbR https://imgur.com/5pFXFbR. Apparently equality in Go is no longer
by miga 6y ago
It is very confusing that equality in JavaScript is not transitive: https://imgur.com/5pFXFbR https://imgur.com/5pFXFbR.
Apparently equality in Go is no longer transitive either...
- jerf 6y agoI think I'd need to see an example of that to be sure. What's talked about here doesn't really lead to that, because in these examples, values are getting passed through a function call boundary. There's a lot of rules in Go for what == does, and maybe there's something non-transitive in there, but I'd definitely need to see someone prove it on https://play.golang.org/ https://play.golang.org/ to be sure. Offhand I can't think of how to make one.
- morelisp 6y agoThere is a nontransitive example heading this very comment section: var b *bytes.Buffer var r io.Reader = b fmt.Printf("r == nil: %v; b == nil: %v; r == b: %v\n", r == nil, b == nil, r == b) r == nil: false; b == nil: true; r == b: true
- jerf 6y agoThank you, I missed that.
- cratermoon 6y agoIt's not confusing if you understand that r is pointer that points to something and b is that pointer, which is nil. So of course r (the variable containing a pointer) isn't nil, b (the pointer) is nil, and r is the pointer b.
- Dylan16807 6y agoPointers don't normally compare equal to the thing they're pointing to. If it's acting as a proxy it's confusing the specific way that some things get proxied and other things don't.
- catlifeonmars 6y agoAgree, r == b in this example is confusing because in all other instances, the interface{} is a kind of box type. That box type is implicitly unwrapped in the r == b comparison. Although more verbose, I’d be happy with having to do something like this for comparison: b2, ok := r.(*bytes.Buffer) equal := ok && b2 == b
- cratermoon 6y agolaughs in C
- nicoburns 6y agoJavaScript is fine so long as you stick to === rather than ==. At work, == is completely banned and tjis is enforces by CI.
- aikah 6y ago> It is very confusing that equality in JavaScript is not transitive: https://imgur.com/5pFXFbR https://imgur.com/5pFXFbR. There are 2 types of equality in JS, shallow (==) and 'strict' (===). It's fine because JS is a dynamic, weakly typed language. Go isn't supposed to be.