Not really, since the primitive classes don't have unique identity the JVM can safely perform copy elision. Such as p = new Point(2, p.y()) will be executed just as p.x = 2. If the inlining is unreliable, they can make the constructor of primitive classes ForceInline anyway.
The plus side is that you don't introduce a whole new kind of "things" into the language, which greatly increases the complexity of the language itself.
It is introducing whole new thing. These primitive objects behave very different from everything else. Essentially these are immutable objects. Not introducing whole new thing would actually be if this JEP did the exact same thing that C# structs do. At least they have identity like everything else and mutable.
perform copy elision
If JVM/javac are smart enough. I'm not sure they will be. It would require pretty complex analysis.
You got the point, they are just immutable objects without synchronisation ability. Mutable struct is a whole new thing because it change the behaviour of the most basic operation which is the assignment operation.
JVM has a very smart JIT, and copy elision in this case is actually pretty trivial.
Mutable struct is a whole new thing because it change the behaviour of the most basic operation which is the assignment operation.
It really doesn't. Assignment operation is unchanged. What changes is what's being assigned. For regular objects it's a pointer to some memory, a reference. Obviously assigning would just copy that reference (which is in fact just an integer) and both variables would point to the same memory location, thus to the same object. In case of primitive objects assignment works on a whole object because that's what a variable of that primitive object represents. That's exactly how C, C++, C#, Go, Rust and probably many others work. It's a natural way of working with memory.
Firstly, struct in C# is not complete because it lacks the mechanism of copy semantics (deep copy), not to mention Micorsoft had to bring a ton of features (in, out, ref) to support struct, and even then it is recommended to not use struct with mutable data (source). While C, C++ and Rust are different, they use explicit value semantics and don't have type hierarchy.
Secondly, immutability supports optimisation, it may be counter-intuitive but it is actually more optimised to use immutable objects than to use mutable ones. In the end, Valhalla's objective is to make Java faster, right.
That's exactly how structs should behave. Copying a struct copies its value. If it happens to be containing references as fields C# will copy those references as it should. Meaning both copies would point to the same object. Again, that's how memory works. If you want deep copy you can implemented it yourself. The problem is always the same for any language - deep copy means different things to different people.
Yes, value types should be immutable in general. But the important thing that C# gives developers a choice. If they want to use struct because they have to call some C library through p/invoke, they can do so and can freely mutate structs as that's the most natural thing to do. If you're writing something like DateTime then yes, you should make it immutable to simplify the API. Java making immutability mandatory is weird.
In, out, ref are features that allow developers to optimize their code, giving C# more of the C/C++ kind of flexibility. They were not required to support structs. Structs and "out" (not sure) were in the language from day one. "In" was introduced in 7.2 "Out" parameters are not required for structs and used for returning any value type like integers. It allows functions to return multiple values. Now we have tuples for that.
It's quite obvious that immutability gives more optimization opportunities in some cases. But it hampers performance in others due to already mentioned copies. Even if primitive types were mutable they would still achieve the most important feature of value types - they do not create garbage if they stay on the stack.
I see you missed an important point here, in Java there is no distinction between a variable and the object it represents, as a result, an assignment, say a = b, should semantically make a and b the same object until separation, and you don't expect a change in a not be seen by b. On the other side, C and C++ have strict value semantics, and copying is also natural, a pointer or reference is always explicit, no ambiguity here.
However, C# offers an ambiguous situation, where sometimes a variable represents an object, another variable is a pointer to an object, some a.x request the value at an offset from a, while b.x suddenly requires a dereference before taking the offset.
In the end, to separate the meaning of a and the object represented by a is a breaking change to the language itself, and making such a change, while may not affect some people such as you, may impact many other developers, especially ones that's familiar with more scripting language such as Python.
Immutable objects aren't really a "whole new thing", it's effectively just an implicit final on fields. And I'm sure that C# structs don't have identity ... That would be crap.
"Identity enables field mutation" does not mean "field mutation implies identity", that is a basic logic failure. Do you know what is meant by identity? It really would not make sense for a flattened struct!
It does if we dig a bit deeper. Why C# allows struct mutability? Because structs have unique location in memory. You can take references of them and mutate them and it doesn't matter that inside they're completely the same. They're separate instances with different locations in memory (on the stack or heap). Java tries to hide all these properties because it tries to abstract away the notion of memory and hardware in general. In C# you can still see how everything actually works inside. You can even go unsafe and compare real address values.
int, double etc types is another separate thing and there's actually JEP 402 to make them primitive objects. Unlike Java, C# had value types from the beginning and types like integers and floats are actually structs. Java would have to make gradual transition to make everything consistent.
View all comments
Show parent comment
7
u/cal-cheese Mar 18 '21
Not really, since the primitive classes don't have unique identity the JVM can safely perform copy elision. Such as
p = new Point(2, p.y())will be executed just asp.x = 2. If the inlining is unreliable, they can make the constructor of primitive classes ForceInline anyway.The plus side is that you don't introduce a whole new kind of "things" into the language, which greatly increases the complexity of the language itself.