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
Joffrey
09/13/2021, 1:41 PM
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
edrd
09/13/2021, 1:48 PM
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
Manuel Dossinger
09/13/2021, 1:59 PM
@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
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