I have scenario like this: ```data class A( va...
# android
a
I have scenario like this:
Copy code
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
Why can't A be a class/interface that class B just inherits from?
u
you can try creating sealed class for it and then derived it for A, B and whatever you want
a
Currently can't change in the data classes since its being used from so many places
by not changing the data classes how we can do the above
j
Then just abstract the metadata to its own super class and make both A and B inherit from it
a
apart from this any trick we can make inside when statement?