O que é a sintaxe quando usando o número comparations em quando declaração

0

Pergunta

Eu tenho este código:

      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")
        }

e eu estava imaginando se eu poderia fazer casos de when instrução com um comparador em vez de um número inteiro. Algo como isto:

      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

Melhor resposta

4

Usar intervalos:

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")
}

Ou você pode usar constantes enum:

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 }

Isto tem a vantagem de que os intervalos são "obrigados" a enumeração si. Se você nunca alterar estes valores, você não terá de alterá-los, possivelmente, de vários locais no seu código.

Edit: alternado de filtro para encontrar (graças a @ArpitShukla, ver comentário abaixo)

2021-11-24 08:43:33

Você pode substituir filter com find. Que faria mais sentido aqui.
Arpit Shukla

Em outros idiomas

Esta página está em outros idiomas

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