I have a code that logically can have only 3 values. But the when assignment is on a Int variable type. What can i do, if i don't have a RecyclerView.ViewHolder object to return if non of the "cases" in the when expression won't happen (a situation that can't be). I can solve it with "ugly" solution that if 'else' so return of one of the RecyclerView.ViewHolder that i'm returning in the existing cases, but i want to know if there is more elegant way to deal with this situation. I have the following code:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType)
{
DataTypesEnum.COUNTRY_ITEM.ordinal -> CountryDataItem.onCreateViewHolder(parent)
DataTypesEnum.LEAGUE_ITEM.ordinal -> LeagueDataItem.onCreateViewHolder(parent)
DataTypesEnum.GAME_ITEM.ordinal -> GameDataItem.onCreateViewHolder(parent)
}
}
the compiler says that :
when' expression must be exhaustive, add necessary 'else' branch