Thuta Learning
Kotlin
BasicProgrammingအခြေခံ

If & When Expressions

Kotlinသင်ခန်းစာ 730 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • if/else ကို expression အဖြစ် ရေးတတ်ဖို့နဲ့ ဒီပုံစံမှာ else branch မဖြစ်မနေ လိုတဲ့ အကြောင်းရင်း ရှင်းပြနိုင်ဖို့
  • when ကို constant၊ range (in)၊ type check (is) နဲ့ subject မပါတဲ့ ပုံစံတွေနဲ့ သုံးနိုင်ဖို့
  • condition တွေ ထပ်နေတဲ့အခါ ပထမဆုံး ကိုက်တဲ့ branch ပဲ run တယ်ဆိုတာ ခန့်မှန်းနိုင်ဖို့
  • enum အပေါ် exhaustive when သုံးပြီး case အသစ်ထပ်တိုးရင် compile error ပေါ်အောင် ဖန်တီးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Program တိုင်းမှာ ရွေးချယ်ရတဲ့ အခါတွေ ရှိပါတယ်။ ပြန်လာတဲ့ ဧည့်သည်ကို welcome message ပြပြီး အသစ်ဆိုရင် sign-up form ပြတာ၊ ကလေးလက်မှတ်၊ လူကြီးလက်မှတ်၊ အသက်ကြီးသူ လက်မှတ်ဆိုပြီး ဈေးခွဲတာမျိုးပေါ့။ Kotlin က ဒီအတွက် if နဲ့ when ဆိုတဲ့ tool နှစ်ခု ပေးထားပါတယ်။ if ကို နှစ်ခုအကြား ရွေးတဲ့ ဆုံးဖြတ်ချက်အတွက်၊ when ကို case အများကြီးထဲက တစ်ခုရွေးဖို့ သုံးပါတယ်။

Java၊ C ဒါမှမဟုတ် JavaScript ကလာတဲ့သူတွေ အံ့ဩတတ်တဲ့ အချက်က if နဲ့ when နှစ်ခုလုံးဟာ expression ဖြစ်နေတာပါ။ Expression ဆိုတာ value တစ်ခု ထုတ်ပေးတဲ့ code အပိုင်းပါ။ ဒါကြောင့် variable ကို အရင်ကြေညာပြီး branch တစ်ခုချင်းစီထဲမှာ သွား assign လုပ်နေစရာ မလိုဘဲ val label = if (...) a else b လို့ ရေးလို့ရပါတယ်။ Variable ကို ဖန်တီးတာ၊ type သတ်မှတ်တာ၊ တန်ဖိုးထည့်တာ တစ်ကြောင်းတည်းနဲ့ ပြီးသွားပြီး branch တစ်ခုမှာ assign မေ့သွားတာမျိုး မဖြစ်နိုင်တော့ပါဘူး။

Problem → Why → Solution

ပြဿနာ: မှားတဲ့ branch က အနိုင်ရနေတယ်

Report card တစ်ခုကို စဉ်းစားကြည့်ပါ။ 90 နဲ့အထက်က A၊ 80 ကနေ 89 က B စသဖြင့်ပေါ့။ စကားနဲ့ ပြောရင် "ခိုင်လုံတဲ့ mark မှန်သမျှ" ဆိုတဲ့ အကျယ်ဆုံး range 0..100 ကို အရင်ရေးမိတယ်ဆိုပါစို့။ Program က compile ဖြစ်ပါတယ်၊ run လည်း ဖြစ်ပါတယ်၊ ဒါပေမယ့် 95 ရတဲ့ ကျောင်းသားကို "Needs practice" လို့ ပြပါတယ်။ Crash မဖြစ်သလို warning လည်း မပေါ်လို့ တကယ့်ကျောင်းသားက တိုင်မှ bug ကို သိရတာမျိုး ဖြစ်နိုင်ပါတယ်။

grade-bug.kt
fun main() {
    val score = 95
    val grade = when (score) {
        in 0..100 -> "Needs practice"   // matches every valid mark, so it always wins
        in 90..100 -> "A"                // never reached
        in 80..89 -> "B"                 // never reached
        else -> "Invalid mark"
    }
    println(grade)                       // Needs practice
}

ဘာကြောင့် compiler က မတားတာလဲ

when ဟာ branch တွေကို အပေါ်ကနေ အောက်ကို စစ်ပြီး ပထမဆုံး ကိုက်တဲ့ branch မှာ ရပ်လိုက်ပါတယ်။ Compiler က သင့် range တွေကို တစ်ခုနဲ့တစ်ခု နှိုင်းယှဉ်မစစ်လို့ အကျယ်ကြီး အရင်ရောက်နေရင် အောက်က branch အားလုံးကို တိတ်တိတ်လေး မျိုချလိုက်ပါတယ်။ ဒီလို အမျိုးအစား bug မျိုး တခြားလည်း ရှိပါတယ်၊ if ကို expression အဖြစ်သုံးပြီး else မေ့တာ၊ enum ကို value အသစ်ထပ်တိုးပြီး when တစ်ခုမှာ branch ထပ်မထည့်မိတာတွေပါ။ အဲဒီနှစ်မျိုးကိုတော့ Kotlin က compile error အဖြစ် တားပေးတာမို့ expression ပုံစံတွေကို ကြိုက်သင့်တဲ့ အကြောင်းရင်း ဖြစ်ပါတယ်။

Core concept: if နဲ့ when က value ထုတ်ပေးတယ်

Kotlin မှာ if ဟာ expression ပါ။ val ရဲ့ ညာဘက်မှာ ဒါမှမဟုတ် return နောက်မှာ သုံးလိုက်ရင် run သွားတဲ့ branch ရဲ့ တန်ဖိုးက if တစ်ခုလုံးရဲ့ တန်ဖိုး ဖြစ်သွားပါတယ်။ Branch က { } block ဖြစ်ရင် block ထဲက နောက်ဆုံး expression က တန်ဖိုးဖြစ်လို့ return keyword မလိုပါဘူး။ (a ? b : c) ဆိုတဲ့ ternary operator မရှိပါဘူး၊ if က အဲဒီအလုပ်ကို လုပ်ပေးနေလို့ပါ။ ရလဒ်ဟာ လမ်းကြောင်းတိုင်းမှာ ရှိရမှာမို့ expression အဖြစ်သုံးတဲ့ if မှာ else ပါရပါမယ်။ val result = if (score >= 50) "Passed" လို့ ရေးရင် kotlinc က "'if' must have both main and 'else' branches when used as an expression." လို့ ပြန်ပြောပါတယ်။

when ကတော့ multi-way version ပါ။ subject တစ်ခုကို ယူပြီး branch တစ်ခုချင်းစီရဲ့ condition နဲ့ အပေါ်ကနေ အောက်ကို နှိုင်းယှဉ်ကာ ပထမဆုံး ကိုက်တဲ့ branch ကို run ပါတယ်။ Condition က constant (3)၊ ကော်မာနဲ့ ခြားထားတဲ့ constant အများကြီး (6, 7)၊ range စစ်တာ (in 90..100)၊ type စစ်တာ (is String) ဖြစ်နိုင်သလို subject မထည့်ဘဲ ရေးရင် ဘယ် boolean expression မဆို ဖြစ်နိုင်ပါတယ်။ Branch တွေကို arrow (->) နဲ့ ရေးပြီး break မလိုသလို အောက်ကို ဆက်မကျသွားပါဘူး၊ branch တစ်ခုပဲ run ပါတယ်။

when ကို expression အဖြစ် သုံးရင် exhaustive ဖြစ်ရပါမယ်၊ ဆိုလိုတာက ဖြစ်နိုင်တဲ့ subject တန်ဖိုး တိုင်းအတွက် branch ရှိရမယ်လို့ ဆိုလိုပါတယ်။ Int ဒါမှမဟုတ် String လို subject မှာ compiler က တန်ဖိုးအားလုံးကို မသိနိုင်လို့ အဆုံးမှာ else ထည့်ရပါတယ်။ enum (နောက်ပိုင်း ကြုံရမယ့် sealed type လည်း အတူတူပါပဲ) ကျတော့ compiler က case အားလုံးကို သိတာမို့ အကုန်ရေးထားရင် else မလိုတော့ပါဘူး။ ဒါက ထင်ထားတာထက် အသုံးဝင်ပါတယ်၊ နောက်မှ enum entry အသစ်ထပ်တိုးရင် အဲဒီ enum ကို သုံးထားတဲ့ when တိုင်း compile မဖြစ်တော့ဘဲ ပြင်ရမယ့် နေရာစာရင်းကို compiler က ပေးလိုက်သလို ဖြစ်သွားပါတယ်။ Kotlin 2.1 မှာ enum အပေါ် when ကို statement အဖြစ်ရေးတာတောင် case ကျန်နေရင် error ပေးပါတယ်။

ပုံစံဘယ်အချိန် သုံးမလဲelse လိုလား
if (cond) a else bနှစ်ခုအကြား ရွေးတာvalue အဖြစ်သုံးရင် လိုတယ်
when (x) { 1 -> ... }subject တစ်ခုကို case အများကြီးနဲ့ နှိုင်းတာenum/sealed အကုန်မကာမချင်း လိုတယ်
when { cond -> ... }အချင်းချင်း မဆိုင်တဲ့ condition တွေကို အစဉ်လိုက် စစ်တာexpression အဖြစ်သုံးရင် လိုတယ်
မှတ်ထားရမယ့် ပုံစံ: လမ်းညွှန်ဆိုင်းဘုတ်
  1. condition ဒါမှမဟုတ် subject ကို တွက်
  2. branch တွေကို အပေါ်ကနေ အောက် စစ်
  3. ပထမ ကိုက်တာ run၊ ကျန်တာ ကျော်
  4. branch ရဲ့ နောက်ဆုံး value က ရလဒ်

အစီအစဉ်က အရေးကြီးပါတယ်၊ ကျဉ်းတဲ့ case တွေကို အရင်ထားပြီး အကုန်ဖမ်းတဲ့ else ကို အဆုံးမှာ ထားပါ။

အထွေထွေ ပုံစံ

kotlin
val result = if (condition) valueA else valueB

val result = when (subject) {
    constant, otherConstant -> valueA
    in range -> valueB
    is Type -> valueC
    else -> fallback
}

ပထမပုံစံက နှစ်ခုအကြား ရွေးတာ၊ ဒုတိယပုံစံက subject တစ်ခုကို constant၊ range၊ type နဲ့ နှိုင်းတာပါ။ subject ကို ဖြုတ်ပြီး when { condition -> value } လို့ ရေးရင် boolean condition တွေကို အစဉ်လိုက်စစ်တဲ့ ပုံစံ ဖြစ်သွားပါတယ်။ တစ်ခုချင်းစီဟာ value ကို val ထဲ တိုက်ရိုက် ထည့်နိုင်ပါတယ်။

Example: mark တစ်ခုကို grade ပေးခြင်း

ဒီ program က score တစ်ခုကို နည်းသုံးမျိုးနဲ့ အဆင့်သတ်မှတ်ပါတယ်။ pass/fail အတွက် တစ်ကြောင်းတည်း if expression၊ စာလုံး grade အတွက် range တွေပေါ်က when၊ ပြီးတော့ နောက်ဆုံး line က တန်ဖိုးဖြစ်တဲ့ block ပါတဲ့ if တစ်ခုပါ။ Output ကို မ run ခင် ကိုယ်တိုင် ခန့်မှန်းကြည့်ပါ၊ score 82 ဆိုတာ ဘယ်လိုထွက်မလဲ။

Grading.kt
fun main() {
    val score = 82

    val result = if (score >= 50) "Passed" else "Failed"
    println(result)

    val grade = when (score) {
        in 90..100 -> "A"
        in 80..89 -> "B"
        in 70..79 -> "C"
        else -> "Needs practice"
    }
    println("Grade: $grade")

    val message = if (score >= 80) {
        val bonus = score - 80
        "Merit, $bonus points above the bar"
    } else {
        "Keep practising"
    }
    println(message)
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. if as a valueval result = if (score >= 50) "Passed" else "Failed"

    if ရဲ့ ရလဒ်ကို result ထဲ တိုက်ရိုက်ထည့်ပါတယ်။ 82 >= 50 မှန်လို့ "Passed" ရပြီး result ရဲ့ type ကို Kotlin က String လို့ infer လုပ်ပါတယ်။ else ကို ဖြုတ်လိုက်ရင် compile error ပါ။

  2. Range branchin 80..89 -> "B"

    in 80..89 က score ဟာ 80 ကနေ 89 အထိ (နှစ်ဖက်စလုံး ပါဝင်) ဖြစ်လားလို့ မေးတာပါ။ 82 က ပထမ branch (90..100) မကိုက်၊ ဒုတိယမှာ ကိုက်လို့ "B" ရပါတယ်။ အောက်က branch တွေကို မစစ်တော့ပါဘူး။

  3. Catch-allelse -> "Needs practice"

    Int subject မှာ ဖြစ်နိုင်တဲ့ တန်ဖိုးအားလုံးကို ကိုယ်တိုင် စာရင်းမချနိုင်လို့ else က ကျန်တဲ့ အားလုံးကို ဖမ်းပါတယ်။ expression အဖြစ်သုံးတဲ့ when မှာ ဒါမပါရင် compile error ပါ။

  4. Block with a valueval bonus = score - 80

    if branch တစ်ခုက { } block ဖြစ်ရင် ထဲမှာ statement တွေ အများကြီး ရေးလို့ရပါတယ်။ ဒီ line က local variable bonus ကို ဖန်တီးတာပါ၊ branch ပြီးရင် ဒီ variable ကို အပြင်ကနေ မမြင်ရတော့ပါဘူး။

  5. Last expression"Merit, $bonus points above the bar"

    Block ရဲ့ နောက်ဆုံး expression က if တစ်ခုလုံးရဲ့ တန်ဖိုးဖြစ်လို့ return မလိုပါဘူး။ String template ကြောင့် $bonus ကို 2 နဲ့ အစားထိုးပြီး "Merit, 2 points above the bar" ရပါတယ်။

You should see
Passed
Grade: B
Merit, 2 points above the bar
score = 82 အတွက် when က branch ရွေးပုံ
  1. score က 82
  2. in 90..100 လား၊ မဟုတ်ဘူး
  3. in 80..89 လား၊ ဟုတ်တယ်
  4. "B" ကို ထုတ်ပေး၊ ကျန် branch တွေ ကျော်

branch ကိုက်ရင်

Run it and use its value

ဘာမှ မကိုက်ရင်

else runs, or compile error if else is missing

ကော်မာ၊ is နဲ့ subject မပါတဲ့ when

branch တစ်ခုမှာ condition အများကြီးကို ကော်မာနဲ့ ခြားရေးရင် "ဒီထဲက တစ်ခုခု ကိုက်ရင်" လို့ ဆိုလိုပါတယ်။ is Type -> ဆိုတာ subject ရဲ့ type ကို စစ်တာပါ၊ ကိုက်ရင် အဲဒီ branch အတွင်းမှာ cast မလိုဘဲ အဲဒီ type အဖြစ် တိုက်ရိုက်သုံးလို့ရပါတယ် (smart cast)။ အောက်က describe() မှာ value.length ကို is String branch ထဲမှာပဲ ခေါ်လို့ရတာ ကြည့်ပါ။

subject မပါတဲ့ when { } ကတော့ else if အစီအစဉ်ရှည်ကြီးကို ပိုသပ်သပ်ရပ်ရပ် ရေးတာပါ။ branch တစ်ခုစီက ကိုယ်ပိုင် boolean condition ဖြစ်ပြီး ပထမဆုံး true ဖြစ်တာ run ပါတယ်။ အောက်က band() မှာ 90 ကနေ အောက်ဘက်ကို ဆင်းသွားတဲ့ အစီအစဉ်ကို သတိထားပါ။ ဒီ threshold ပုံစံက နောက်မှ ကိန်းပြည့်ကနေ ဒသမကိန်းကို ပြောင်းသုံးတဲ့အခါလည်း မှန်နေပါတယ်၊ in 80..89 လို range မှာတော့ 89.5 က ကျန်ခဲ့မှာပါ။

WhenForms.kt
fun describe(value: Any): String = when (value) {
    0, 1 -> "tiny number"
    is Int -> "int, next is ${value + 1}"
    is String -> "text of length ${value.length}"
    else -> "something else"
}

fun band(score: Int): String = when {
    score >= 90 -> "A"
    score >= 80 -> "B"
    score >= 70 -> "C"
    else -> "Needs practice"
}

fun main() {
    println(describe(1))
    println(describe(41))
    println(describe("kotlin"))
    println(describe(2.5))
    println(band(95))
    println(band(80))
    println(band(12))
}

ထွက်လာမယ့် output က tiny number၊ int, next is 42၊ text of length 6၊ something else၊ ပြီးတော့ A၊ B၊ Needs practice ပါ။ describe(1) က ပထမ branch (0, 1) မှာ ရပ်သွားတာမို့ is Int branch ကို မရောက်ပါဘူး၊ branch အစီအစဉ်ရဲ့ သက်ရောက်မှုကို ထပ်မံ တွေ့ရတာပါ။

enum အပေါ် exhaustive when

Order status လို တန်ဖိုးအနည်းငယ်ပဲ ရှိတဲ့ အရာကို enum class နဲ့ ဖော်ပြပြီး when နဲ့ ကိုင်တွယ်ရင် else မလိုဘဲ အကုန်ကို ရေးလို့ရပါတယ်။ အောက်က code ကို ကြည့်ပါ၊ else မပါပါဘူး၊ ဒါပေမယ့် case သုံးခုလုံး ကိုင်ထားလို့ compile ဖြစ်ပါတယ်။ Status.entries က enum ရဲ့ case အားလုံးကို list အဖြစ် ပေးပါတယ်။

OrderStatus.kt
enum class Status { PENDING, SHIPPED, DELIVERED }

fun label(status: Status): String = when (status) {
    Status.PENDING -> "Waiting for the warehouse"
    Status.SHIPPED -> "On the way"
    Status.DELIVERED -> "At your door"
}

fun main() {
    for (status in Status.entries) {
        println("$status -> ${label(status)}")
    }
}

case အသစ်ထပ်တိုးကြည့်ပါ

enum ထဲကို CANCELLED ထပ်ထည့်ပြီး label() ကို မပြင်ဘဲ compile လုပ်ရင် kotlinc 2.1.21 က "'when' expression must be exhaustive. Add the 'CANCELLED' branch or an 'else' branch." လို့ error ပေးပါတယ်။ ဒါက ကောင်းတဲ့ error ပါ၊ ပြင်ရမယ့် နေရာကို အတိအကျ ပြောပြနေတာပါ။ ဒါပေမယ့် else -> ... ကို ထည့်ပြီး ဆိုင်းငံ့လိုက်ရင် နောက်တစ်ခါ case အသစ်ထပ်တိုးတဲ့အခါ ဒီအကာအကွယ်ကို ဆုံးရှုံးသွားပါလိမ့်မယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Backend code မှာ HTTP status code ကို error category တစ်ခုအဖြစ် ပြောင်းတာ၊ order နဲ့ payment status ကို user ကြည့်ရမယ့် စာသား ပြောင်းတာ၊ အသက်ကျော်၊ အလေးချိန်၊ ဝယ်ယူမှု ပမာဏနဲ့ ဈေးနှုန်းအဆင့် ရွေးတာတွေအားလုံး when နဲ့ ရေးလေ့ရှိပါတယ်။ Android app တွေမှာတော့ screen state (loading၊ success၊ error) ကို sealed type နဲ့ ဖော်ပြပြီး when နဲ့ ဘာကို ဆွဲမလဲ ဆုံးဖြတ်ကြပါတယ်။

Expression ပုံစံက code review မှာလည်း အကျိုးရှိပါတယ်။ val label = when (status) { ... } ဆိုတဲ့ တစ်ခုတည်းသော ဖြစ်စဉ်နဲ့ label ရဲ့ ဖြစ်နိုင်ခြေအားလုံးကို မြင်ရလို့ var label = "" လို့ ကြေညာပြီး branch တစ်ခုချင်းစီမှာ assign လုပ်တာထက် ဖတ်ရလွယ်ပါတယ်၊ assign မေ့ခဲ့တဲ့ branch ကိုလည်း compiler က ဖမ်းပေးပါတယ်။ ပရောဂျက်ကြီးတွေမှာ enum နဲ့ sealed type အပေါ် else မသုံးဘဲ when ရေးထားတာကို team တွေက ရည်ရွယ်ချက်ရှိရှိ စံပြုထားကြတာ ရှိပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ model မှာ case အသစ်ထပ်တိုးတဲ့အခါ ထိခိုက်မယ့် code ကို compiler က ရှာပေးလို့ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. အကျယ်ဆုံး range ကို အရင်ရေးမိတာ

in 0..100 ကို in 90..100 အပေါ်မှာ ထားရင် 95 တောင် ပထမ branch ကို ရောက်ပါတယ်။ Compiler က warning မပေးပါဘူး၊ ဒါကြောင့် branch တွေကို အကျဉ်းဆုံးကနေ အကျယ်ဆုံးအထိ စီပါ၊ ဒါမှမဟုတ် subject မပါတဲ့ when နဲ့ >= ကို အကြီးကနေ အငယ်စီပြီး ရေးပါ။ စမ်းတဲ့အခါလည်း boundary တန်ဖိုး (90၊ 89၊ 0) ကို စစ်ပါ။

2. expression အဖြစ်သုံးတဲ့ if မှာ else မေ့တာ

val result = if (score >= 50) "Passed" က "'if' must have both main and 'else' branches when used as an expression." နဲ့ compile မဖြစ်ပါဘူး။ else if ကြိုးရှည်ကြီးရဲ့ နောက်ဆုံးမှာ else မပါတာလည်း အတူတူပါပဲ။ တန်ဖိုးမလိုဘဲ print လုပ်ရုံပဲဆိုရင်တော့ if ကို statement အဖြစ် ရေးပါ၊ ဒါဆို else မလိုပါဘူး။

3. enum when မှာ else ထည့်ပြီး compiler ရဲ့ စစ်ဆေးမှုကို ပိတ်လိုက်တာ

Compile error ပျောက်သွားအောင် else -> "Unknown" ထည့်လိုက်ရင် နောက်တစ်ခါ enum entry အသစ်ထပ်တိုးတဲ့အခါ ဘယ် when ကိုမှ ပြင်ဖို့ compiler က မပြောတော့ဘဲ အသစ်က "Unknown" ထဲ တိတ်တိတ်ကျသွားပါတယ်။ enum ဒါမှမဟုတ် sealed type အပေါ်မှာ case တိုင်းကို ကိုယ်တိုင်ရေးပါ၊ else ကို တကယ် မသိနိုင်တဲ့ ပြင်ပ တန်ဖိုးတွေအတွက်ပဲ သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • တန်ဖိုးတစ်ခု ထုတ်ဖို့ ရည်ရွယ်ရင် var + assign ထက် val x = if/when ကို ရွေးပါ၊ variable ကို ပြောင်းလို့ မရတော့ဘဲ branch တိုင်းမှာ တန်ဖိုးရှိဖို့ သေချာပါတယ်။
  • condition သုံးခုထက်ပိုရင် if ကို else if နဲ့ ဆက်မရေးဘဲ when ကို ပြောင်းပါ။
  • enum နဲ့ sealed type အပေါ် when မှာ else မထည့်ပါနဲ့၊ case အသစ် ထပ်တိုးရင် compiler က ပြင်ရမယ့်နေရာကို ပြောပြပါစေ။

Mini challenge

Mini challenge: ပို့ဆောင်ခ တွက်ချက်စက်

Parcel ပို့ဆောင်ခ တွက်တဲ့ program တစ်ခု ရေးပါ။ ပို့မယ့် ဇုန်ကို enum နဲ့ ဖော်ပြပြီး else မသုံးဘဲ when နဲ့ ဇုန်ပေါ်မူတည်တဲ့ အခြေခံ ဈေးကို ရွေးပါ၊ အလေးချိန်ပေါ်မူတည်တဲ့ အတန်းအစားနဲ့ အလေးချိန်ပိုရင် ထပ်ကောင်းကြေး ထည့်ပါ။

  • enum class Zone မှာ LOCAL၊ REGIONAL၊ INTERNATIONAL သုံးခု ထားပါ
  • shippingFee(zone, weightKg) က zone ကို else မပါတဲ့ when နဲ့ အခြေခံဈေး 5၊ 12၊ 30 ရွေးပြီး 20kg ထက်ပိုရင် 15 ပေါင်းထည့်ပါ
  • weightClass(weightKg) က subject မပါတဲ့ when နဲ့ invalid (1 ထက်နည်း)၊ light (5 အထိ)၊ standard (20 အထိ)၊ heavy ကို ပြန်ပါ
  • main မှာ LOCAL 3kg၊ REGIONAL 12kg၊ INTERNATIONAL 25kg တို့အတွက် weight class နဲ့ ပို့ဆောင်ခကို print ပါ
Shipping.kt
enum class Zone { LOCAL, REGIONAL, INTERNATIONAL }

fun weightClass(weightKg: Int): String {
    // TODO: subject-less when, thresholds from smallest to largest
}

fun shippingFee(zone: Zone, weightKg: Int): Int {
    // TODO: val base = when (zone) { ... } with no else
    // TODO: val surcharge = an if expression for weights over 20
    // TODO: return the sum
}

fun main() {
    // TODO: print "<class>: <fee>" for (LOCAL, 3), (REGIONAL, 12), (INTERNATIONAL, 25)
}

ရလဒ်: light: 5 standard: 12 heavy: 45

အရိပ်အမြွက် ကြည့်ရန်

weightClass() ရဲ့ when ကို weightKg < 1 -> "invalid" နဲ့ စပါ၊ ပြီးရင် weightKg <= 5၊ weightKg <= 20 လို့ တိုးသွားပါ။ shippingFee() မှာ when (zone) မှာ else မထည့်ပါနဲ့၊ case သုံးခု ကိုင်ထားရင် compiler က လက်ခံပါတယ်။

အဖြေ ကြည့်ရန်
Shipping.kt
enum class Zone { LOCAL, REGIONAL, INTERNATIONAL }

fun weightClass(weightKg: Int): String = when {
    weightKg < 1 -> "invalid"
    weightKg <= 5 -> "light"
    weightKg <= 20 -> "standard"
    else -> "heavy"
}

fun shippingFee(zone: Zone, weightKg: Int): Int {
    val base = when (zone) {
        Zone.LOCAL -> 5
        Zone.REGIONAL -> 12
        Zone.INTERNATIONAL -> 30
    }
    val surcharge = if (weightKg > 20) 15 else 0
    return base + surcharge
}

fun main() {
    println("${weightClass(3)}: ${shippingFee(Zone.LOCAL, 3)}")
    println("${weightClass(12)}: ${shippingFee(Zone.REGIONAL, 12)}")
    println("${weightClass(25)}: ${shippingFee(Zone.INTERNATIONAL, 25)}")
}

Quick Quiz 1 / 3

val n = 15 ဖြစ်ပြီး when (n) { in 1..20 -> "small"; in 10..30 -> "medium"; else -> "large" } ကို ရေးထားရင် ဘာ ရမလဲ။

Quick Quiz 2 / 3

x တစ်ခု Int ဖြစ်တယ်ဆိုပါစို့။ အောက်ပါထဲက ဘယ် line က compile မဖြစ်ဘူးလဲ။

Quick Quiz 3 / 3

enum class Level { LOW, MID, HIGH } အတွက် else မပါတဲ့ when expression တစ်ခု ရှိပါတယ်၊ Level.CRITICAL ကို နောက်မှ ထပ်ထည့်လိုက်ရင် ဘာဖြစ်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • if နဲ့ when က expression တွေဖြစ်လို့ တန်ဖိုးကို val ထဲ တိုက်ရိုက်ထည့်နိုင်ပါတယ်၊ ဒီအခါ else (သို့မဟုတ် case အားလုံး) မဖြစ်မနေ လိုပါတယ်။
  • when က branch တွေကို အပေါ်ကနေ အောက်စစ်ပြီး ပထမကိုက်တာကိုပဲ run ပါတယ်၊ ဒါကြောင့် ကျဉ်းတဲ့ case တွေကို အရင်ထားပါ။
  • in range၊ is type၊ ကော်မာနဲ့ subject မပါတဲ့ ပုံစံတွေက when ကို အလွန် ပြောင်းလွယ်စေပါတယ်။
  • enum နဲ့ sealed type အပေါ်က else မပါတဲ့ when က case အသစ်ထပ်တိုးရင် compile error ပေးလို့ ကာကွယ်ပေးပါတယ်။

Kotlin language guide — Kotlin

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

If & When Expressions | Thuta Learning