data class A(
val metadata: String
){
data class B(
val metadata: String
)
}
fun updateData(data: Any){
when(data){
is A -> {// update A metadata value
}
is A.B -> {// update B metadata value
}
}
}
want to update the value of
metadata
for both data class by using the same logic and don't want to write it twice inside when statement. How can I do it by writing the logic only once?
j
Joakim Forslund
01/19/2022, 1:15 PM
Why can't A be a class/interface that class B just inherits from?
u
Umar Ata
01/19/2022, 2:59 PM
you can try creating sealed class for it and then derived it for A, B and whatever you want
a
Ali
01/19/2022, 3:32 PM
Currently can't change in the data classes since its being used from so many places
Ali
01/19/2022, 3:33 PM
by not changing the data classes how we can do the above
j
Joakim Forslund
01/19/2022, 3:35 PM
Then just abstract the metadata to its own super class and make both A and B inherit from it
a
Ali
01/19/2022, 3:45 PM
apart from this any trick we can make inside when statement?