Not sure if this is the right channel, but: is the...
# getting-started
m
Not sure if this is the right channel, but: is there something new on that topic https://discuss.kotlinlang.org/t/what-is-an-idiomatic-way-to-create-a-read-only-wrapper-for-a-class/10929 ? I.e., I'd like to have a class
data class MutableClass(var my1, var my2, var my3...)
and a fitting
data class ImmutableClass(val my1, val my2, val my3...)
. So the only difference is, that all members of one class are
var
and the members of the other one are
val
and of course I don't want to write the list of members twice 🙂
j
I don't believe there is any automatic way to do this at the moment. Also, I'd like to point out that immutable != read-only, so even automatically converting
var
to
val
in some way via a language feature wouldn't guarantee immutability (it would depend on the implementation of the class itself)
e
You could write a symbol processor to scan your data classes and generate a mutable/immutable version of it, also with functions to convert between one another.
1
m
@Joffrey thanks, this is a good point; I guess I was thinkin in Rust 🙂 @edrd I'd rather not introduce "non-standard" code, but thanks
p
j
Yeah the value class feature is pretty interesting, but I think this is not what the OP is after here, the goal was to write only once the immutable and mutable version of the class, and somehow have both generated from it