Reputation: 1480
What is the best way to cast an object to a certain type of object if it's not null (otherwise, a factory method is called). This is happening inside a BaseAdapter. What's the best way to do it?
val itemView = view as? ItemView ?: factory()
I'm getting the following warning in Android Studio
Unchecked cast: View? to ItemView
Upvotes: 1
Views: 88
Reputation: 22595
You can handle it elegantly and functionally with pattern matching:
val itemView = when(view) {
is ItemView -> view
else -> factory()
}
Upvotes: 1