Raymond
04/04/2025, 12:31 AMreadonly
-like keyword for class properties in Kotlin.
For example, something like this:
class ComponentDTO(
val primaryText: String,
val secondaryText: String? = null,
readonly val category: Category = Category.Informative,
readonly val drop: Drop = Drop.Down
)
Which would internally be equivalent to:
class ComponentDTO(
val primaryText: String,
val secondaryText: String? = null,
) {
val category: Category = Category.Informative
val drop: Drop = Drop.Down
}
I think this could be particularly useful, especially if it can be implemented without breaking binary compatibility when removing the readonly
keyword.
Would love to hear your thoughts or if there's something similar already being considered!Chris Lee
04/04/2025, 1:47 AMval
already provides read-only semantics (the reference can’t be changed).asdf asdf
04/04/2025, 2:29 AMChris Lee
04/04/2025, 2:35 AMespecially if it can be implemented without breaking binary compatibilityData classes shouldn’t be used in an API as they aren’t binary-compatiblity safe. https://kotlinlang.org/docs/api-guidelines-backward-compatibility.html#avoid-using-data-classes-in-your-api Having those
val
properties in the class definition (instead of the constructor) makes them constants - may as well move them into a companion object, as they can no longer be externally set.Raymond
04/04/2025, 2:40 AMRaymond
04/04/2025, 2:45 AMRaymond
04/04/2025, 2:46 AMChris Lee
04/04/2025, 2:47 AMRaymond
04/04/2025, 2:48 AMRaymond
04/04/2025, 2:48 AMRaymond
04/04/2025, 2:49 AMRaymond
04/04/2025, 2:49 AMJoffrey
04/04/2025, 11:26 AMJoffrey
04/04/2025, 11:28 AMJoffrey
04/04/2025, 11:28 AMRaymond
04/04/2025, 12:37 PMJoffrey
04/04/2025, 12:42 PMReplaceWith
clauses etc. It gives you way better evolution possibilities.
I hit these compatibility problems in my STOMP client library for the header classes, and solved it by forcing users to use builders instead. I didn't want the "fluent" Java-style builders, though. Instead, I provided some sort of constructor-looking factory function that takes a lambda, and the receiver in that lambda is a builder interface that has `var`s for things I want to allow to change.
See this example (and other header classes in the same dir) to see what I mean: https://github.com/joffrey-bion/krossbow/blob/main/krossbow-stomp-core/src/commonMain/kotlin/org/hildan/krossbow/stomp/headers/StompConnectedHeaders.ktRaymond
04/04/2025, 12:44 PM