5 ms·
It's still confusing in Java because there are primitive types which == compares by value. Scala gets it right: == does the right thing and there's some operato
by lmm 9y ago
It's still confusing in Java because there are primitive types which == compares by value. Scala gets it right: == does the right thing and there's some operator I never use (I think it's "eq"?) if you really want reference comparison.
- int_19h 9y agoIn Java it's not confusing when you realize that == always compares values (i.e. things that can be stored in variables); it's just that for reference types, the values are references. It'd be more obvious if Java required explicit dereferencing like C++ does, but it's still consistent. Just not convenient.
- lmm 9y ago> In Java it's not confusing when you realize that == always compares values (i.e. things that can be stored in variables); it's just that for reference types, the values are references. It's still confusing. Plenty of "reference types" behave like values in all obvious senses. Why should "foo" be a reference but 45L a value?
- pjmlp 9y agoBecause "foo" is an instance of String, aka subclass of Object, while 45L is a value for the primitive type long. I fail to see the confusion, other than for newbies.
- simon_o 9y agoThat's pretty much saying "it is the way it is", which a) is not even true, see floating point numbers and b) will be obsolete with value types. Needless to say, there is pointless confusion created by Java's design, and there are better approaches available. All you have to do is to adapt the semantic model from reference equality vs. value equality to identity vs. equality. Identity checks whether the "bits" are identical, irregardless of whether the bits are "references" or values, and equality is a user-defined operation. "Foo" equality "Foo" // True "Foo" identity "Foo" // Only true if they point to the same "object" 123 equality 123 // True 123 identity 123 // True Double.NaN equality Double.NaN // False Double.NaN identity Double.NaN // True Which symbols you pick for "equality" and "identity" is largely arbitrary.
- int_19h 9y agoBetter yet, allow identity checks for reference types only. Value types don't have identity, per se, so the operation should be meaningless for them.
- simon_o 9y agoI don't think you can get away with that for theoretical and practical reasons: 1. There is a ton of code out there which does something like def contains(that: Thing): Boolean = this.value identity that || this.value equality that Pretty much every single collection implementation would be broken if this stopped working with value types. Additionally, you would run into issues with floating point numbers which would not be found/retrieved anymore if identity were removed. 2. The idea to define a _sane_ definition of identity/equality across all types is there to avoid the "next-best" option: boxing primitives to wrapper classes which is both slow and has terrible semantics. 3. I don't really think restricting identity to e.g. reference types makes sense given that equality is defined for every type. Either none of them should be available by default, or both should be. There _are_ multiple valid ways to compare to things (consider floating point numbers for a second) and making one more privileged than the other feels wrong.
- lmm 9y ago> Pretty much every single collection implementation would be broken if this stopped working with value types. Maybe collections of values should be different from collections of references. The sensible use cases for the two are quite different. > Additionally, you would run into issues with floating point numbers which would not be found/retrieved anymore if identity were removed. Meh, just allow NaN to compare equal to itself. Equality is supposed to be reflexive. > The idea to define a _sane_ definition of identity/equality across all types is there to avoid the "next-best" option: boxing primitives to wrapper classes which is both slow and has terrible semantics. Unboxed primitives don't have identity, only value equality. They align well with what's being proposed. > I don't really think restricting identity to e.g. reference types makes sense given that equality is defined for every type. Either none of them should be available by default, or both should be. We could build the distinction into the language, so for every type you define you explicitly choose whether it's value or reference. Scala's already halfway there with the class/case class distinction. > There _are_ multiple valid ways to compare to things (consider floating point numbers for a second) Disagree; comparison is so fundamental to most types that it's worth privileging. Using the wrong kind of comparison is a very common source of bugs.
- int_19h 9y agoThat's tautological. The question is not why String behaves as a reference type - obviously, because it is a reference type. The question is, why is String a reference type, when it has such obvious value semantics? And it's a perfectly valid question. The real answer is that they wanted strings to have methods, so it had to be a class, because primitives can't have methods; and Java doesn't have value classes, so it had to be a reference type. So the reason is a deficiency in the language. A better question is, why .NET string is a reference type (with overloaded == to make it behave like a value, even!). It could have easily been a struct. I suspect that this is one of those decisions that were made very early on to basically be like Java because that's what people expect; and then it became impossible to change without major breakage. The real problem is that most implementations that do reference / value type distinction, mix up semantics and implementation. What we really need is the ability to say that something 1) doesn't have a meaningful identity, and 2) is immutable (in a sense that you can't replace individual components of that object; you still can replace the whole object). When you combine these two, you get something that can be implemented as a value type under the hood, but the programmer doesn't need to think about it. They can just continue treating it as a reference, just as they do in Python. There's no way to tell the difference.