As per documentation of When in Kotlin, else is not mandatory if the compiler knows all the values are covered. This is very in case of enums or sealed class but how to do it in case of arrays for numbers 1 to 5 (startRating).
private fun starMapping(startRating: Int): String {
return when (startRating) {
1 -> "Perfect"
2 -> "Great"
3-> "Okay"
4-> "Bad"
5-> "Terrible"
// don't want to add else as I believe it is prone to errors.
}
}
Something similar to this
return when (AutoCompleteRowType.values()[viewType]) {
AutoCompleteRowType.ITEM -> ItemView(
LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_item_info, parent, false))
AutoCompleteRowType.SECTION -> SectionView(
LayoutInflater.from(parent.context).inflate(R.layout.item_venue_autocomplete_section, parent, false)
)
}
else -> throw IllegalArgumentException("Illegal startRating")(this is how it's done on the official kotlin idioms page).elseclause? What errors are you expecting? You would have to write anenumif you want it to be obsolete... Anenumseems more suitable for star ratings thanInts anyway.startRatingto be "Intfrom 1 to 5", and there's no such type in Kotlin.