ฉันมีรหัส:
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")
}
และฉันสงสัยว่าถ้าฉันสามารถทำให้คดีของ when
แถลงการณ์กับ comparator แทนที่จะเป็นเป็นเลขจำนวนเต็ม บางอย่างเหมือนนี้:
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")
}
filter
กับfind
. นั่นฟังดูมีเหตุผลกว่าเหรอยู่ที่นี่