Aká je syntax pri používaní číslo comparations, keď vyhlásenie

0

Otázka

Mám tento kód:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            320 -> StatisticsSettings.SMALL_PHONE
            480 -> StatisticsSettings.LARGE_PHONE
            600 -> StatisticsSettings.SMALL_TABLET
            720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }

a bol som zvedavý, či by som mohol urobiť prípadoch when vyhlásenie s komparátor namiesto integer. Niečo ako toto:

      statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 320 -> StatisticsSettings.SMALL_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 480 -> StatisticsSettings.LARGE_PHONE
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 600 -> StatisticsSettings.SMALL_TABLET
            ScreenHandler.convertPixelsToDp(width, context).toInt()) < 720 -> StatisticsSettings.LARGE_TABLET
            else -> throw IllegalArgumentException("Cannot compute dp")
        }
kotlin
2021-11-23 23:05:51
1

Najlepšiu odpoveď

4

Použitie rozsahy:

val statisticsSettings = when (ScreenHandler.convertPixelsToDp(width, context).toInt()){
  in 0..320 -> StatisticsSettings.SMALL_PHONE
  in 321..480 -> StatisticsSettings.LARGE_PHONE
  in 481..600 -> StatisticsSettings.SMALL_TABLET
  in 601..720 -> StatisticsSettings.LARGE_TABLET
  else -> throw IllegalArgumentException("Cannot compute dp")
}

Alebo by ste mohli používať konštanty vymenovaného typu:

enum class StatisticsSettings(val intRange: IntRange) {
  SMALL_PHONE(0..320),
  LARGE_PHONE(321..480),
  SMALL_TABLET(481..600),
  LARGE_TABLET(601..720)
}

val intRange = ScreenHandler.convertPixelsToDp(width, context).toInt()

val statisticsSettings = StatisticsSettings.values().find { intRange in it.intRange }

To má tú výhodu, že rozsahy sú "viazaný" na enum sám. Ak ste niekedy zmeniť tieto hodnoty, nemusíte ich meniť na prípadne viacerých miestach v kóde.

Edit: prešiel z filtra do nájsť (vďaka @ArpitShukla, pozri komentár nižšie)

2021-11-24 08:43:33

Môžete nahradiť filter s find. Že by bolo rozumnejšie tu.
Arpit Shukla

V iných jazykoch

Táto stránka je v iných jazykoch

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................