I have a List<ParentType>
that contains items from a hodgepodge of types that inherit from ParentType, which I’ll call ChildType1, ChildType2, etc.
Now I need to iterate over this list and call a method doStuff
, which is defined differently in each of the various ChildType classes. The simple myList.forEach {it.doStuff()}
doesn’t work because the items coming out of myList are treated as if they are ParentType instead of their respective ChildTypes.
So what I have now is
myList.forEach {
when(it) {
is ChildType1 -> it.doStuff()
is ChildType2 -> it.doStuff()
is ChildType3 -> it.doStuff()
// and a bunch more
}
}
The when & smart casts do the job, but is this really the best solution? I’d like to think there was a way to do this without a separate line for each of the multitude of ChildTypes, but I can’t seem to figure one out.
Thanks in advance for any insight.