Here is a piece of code
override fun onOptionsItemSelected(item: MenuItem) { when (item.itemId) { R.id.action_settings -> consume { navigateToSettings() } R.id.nav_camera -> drawer.consume { navigateToCamera() } R.id.nav_gallery -> drawer.consume { loadGallery() } R.id.nav_slideshow -> drawer.consume { loadSlideshow() } } }
It can be changed to the following code with inferred return type
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { R.id.action_settings -> consume { navigateToSettings() } R.id.nav_camera -> drawer.consume { navigateToCamera() } R.id.nav_gallery -> drawer.consume { loadGallery() } R.id.nav_slideshow -> drawer.consume { loadSlideshow() } }
Although in this case the compiler mentions that “when expression must be exhaustive add necessary else branch”
I added the line below and the error wen away
else -> super.onOptionsItemSelected(item)
Shouldn’t we have the same behaviour in both cases for more consistency?