Hello guys. In the following class: ```sealed cla...
# announcements
u
Hello guys. In the following class:
Copy code
sealed class GUIModel {
    
    abstract val id: Id
    
    interface Selectable {
        val isSelected : Boolean
    }
    
    data class Model1(override val id: Id, override val isSelected: Boolean) : GUIModel(), Selectable
    data class Model2(override val id: Id, override val isSelected: Boolean) : GUIModel(), Selectable
    data class Model3(override val id: Id) : GUIModel()
}
is there a way to create a copy of the instance with updated
isSelected
field? Without having to check the type:
Copy code
fun GUIModel.createCopyWithUpdatedSelection(isSelected: Boolean) : GUIModel {
        return when(this) {
            is Model1 -> this.copy(isSelected = isSelected)
            is Model2 -> this.copy(isSelected = isSelected)
            is Model3 -> this
        }
    }
and without switching to
var
in
Selectable
interface. Thanks!
m
You can add the method as an abstract method to GUIModel.