Kotlin is not a good language for that, in my opinion. It performs many implicit allocation under the hood (lambda, capturing variable, coroutine invocation), nor it makes explicit when and how the memory has to be allocated.
“Reduce allocation” and “improve performance” do not provide any control over allocation.
No, I don’t.
Regular class cannot replace value class.
I don’t propose to change the current var, I’m asking if Kotlin needs a new var and how this differs.
I agree, this point require more considerations.
But allows a mutable state in an immutable type can be really hard to justify.
Please consider the imaginary numbers:
This is a reasonable implementation:
value class Imaginary(val r: Double, val i: Double)
Is this a reasonable implementation?
value class Imaginary(var r: Double, var i: Double)
How I can mutate an immutable value (like a number)?
So, if it is impossible to define a mutable state in an immutable type, we can define it as:
value class Imaginary(r: Double, i: Double)
val is superfluous and there is no var here.