Repository navigation
Better Immutability in Kotlin -- Multi-Field Value Classes #473
Replies: 14 comments 31 replies
What about Pair and Triple? or a In fact, I heavily disagree with the move to deprecate |
|
Why have abstract value classes at all? Their restrictions sound remarkably close to an interface. What does it give you that an interface doesn't?
but I don't see why that's a useful or necessary concept. However, one cool part is that some You also go on to say:
but again, can't an interface do that pretty well already? |
|
Can you elaborate further on value objects? It seems rather unnecessary to me. I'd much rather just blanket prohibit |
Conceivably, could they be optimized eventually? Especially on non-JVM platforms, where Valhalla doesn't affect things. This proposal doesn't rule that out in the future, right? |
|
While MFVC ergonomic update is being designed, and even after it's added, I think having a |
|
(for
This is generally but not always true. If the delegate is already a property, is stateless, or the delegate can be inlined, no additional field is needed. That said, I can't come up with any convincing use-cases where using such delegates would be important, so maybe it's not important. |
|
(for migration)
In my experience and my own use, most |
|
I think it's necessary to have a way of defining "requires identity" for user code, and ideally making it type checked. For example, for a parameter that will be synchronized on later. Or a parameter that will be used to create a cleaner or weak ref. Currently, there's no way to enforce this at compile time, so presumably it would be a runtime error, or worse, unexpected undefined behavior depending on the auto-boxing strategy used. Which would be unfortunate, because there would be no way to know that the method you're calling is safe to use with a value object or not. This is all assuming that value classes extend |
|
(on the section "MFVC and ===")
I think this is acceptable. It is already a common behavior in the JVM world: |
If this is possible, I think this will be a massive improvement in developer experience with using Compose. That by itself makes MFVC very compelling for UI development. |
|
I really like the idea of separating the |
|
Can a section be added which details the process for adding a property to an existing value class in a binary-compatible manner? Is it merely providing the constructor overload? Presumably |
|
Hi mates, just a small update that we have not forgotten about your feedback, I just need more time to go through, merge related things together and answer them. Thank you for all your comments, and for your patience ❤️ |
|
We’ve updated the KEEP-0454 to clarify the scope of the first experimental phase of MFVCs planned for Kotlin 2.5. The update talks in more detail on the design around abstract value classes, value objects, and other cases where you asked for refinement. It also talks a bit more on the migration stories and explicitly moves non-phase-I ideas -- such as stable delegation, runtime identity checks, and changes to identity equality -- into a dedicated "Possible Extensions" section. We will be working on bringing some of those to 2.5.20 and 2.6. We will soon also share the KEEP for the second part of the story -- ergonomic updates via |
Uh oh!
There was an error while loading. Please reload this page.
We are publishing the design proposal for Multi-Field Value Classes (MFVCs) in Kotlin -- the first concrete step in our "Value Classes 2.0" story for better immutability (see KEEP-0453 for the general overview).
The core idea is simple: lift the single-property restriction on Kotlin value classes, allowing them to have multiple primary
valproperties. This gives us shallow immutable, identity-less types with structural equality aka "shallow immutable data classes without identity".This looks simple, but (as usual) there are a lot of complicated interactions in the detailed design. If you're interested, go and read the proposal, and (if you want) follow the Call to Action section to give us feedback.
Read the full proposal: KEEP-0454
Related YouTrack issue: KT-77734
All reactions