I have a sealed class containing custom data classes. Some of these data classes store the same variable x like:
sealed class Example {
data class foo1(var timestamp: String?, var model1: Model1?): Example()
data class foo2(var timestamp: String?, var model2: Model2?): Example()
data class foo3(var model3: Model3?): Example()
}
Model3 should also contain a timestamp variable.
What I'm trying to do is something like this:
when(foo) {
is Foo1, is Foo2 -> {
//run same piece of code
foo.timestamp = ts
}
is Foo3 -> {
//run diff piece of code
foo.model3.timestamp = ts
}
}
But I am having issues with this because I am getting unresolved reference with "timestamp" when doing the above on the first "is" block. It only works when I individually reference each type and run the same block of code for each which looks really ugly with more object types.
Is there a way I can do the above and have it work, or any suggestions on if I should restructure anything with my approach to get this to work?