Jérémy CROS
06/14/2021, 1:23 PMsealed class Sealed(open val x: Int) {
data class A(override val x: Int) : Sealed(x)
data class B(override val x: Int) : Sealed(x)
}
private fun copyTest(test: Sealed, y: Int): Sealed {
// "standard" way, it works
return when (test) {
is Sealed.A -> test.copy(x = y)
is Sealed.B -> test.copy(x = y)
}
// Sadly, this does not
return test.copy(x = y)
}
Is the when approach the only one? Or am I missing something?
Thanks!LeoColman
06/14/2021, 1:34 PMcopy
method, it's specific to data classes and thus you need to define it there.LeoColman
06/14/2021, 1:34 PM