Keywords in Kotlin (fun #3)

Aung Ye Htet
1 min readMay 25, 2020

--

ကျွန်တော်တို့ Functionတွေ ရေးသားတဲ့အခါမှာ သူဆီကနေ ပြန်ရလာမဲ့ တန်ဖိုးအတွက် Return Type ကို အမြဲတမ်းလိုလိုထည့်ပေးရပါတယ်။

Kotlin မှာလဲ အတူတူပါပဲ။

ဥပမာ-

ခုနက makeCoffee () မှာဆို return “Coffee” ဆိုတဲ့Code‌လေး။ သူရချင်တာက String Type ဆိုတော့ “Coffee” ဆိုတဲ့ String လေး returnပြန်ပေးရတာ‌ပေါ့ဗျာ။

3. Unit Functions

Kotlin Function တွေမှာက Return Typeကို မဖြစ်‌မနေထည့်ပေး‌နေရတာမျိုး မဟုတ်သလို “return” Keyword ကိုလည်း အမြဲတမ်းထည့်သုံးစရာမလိုပါဘူး။

သူ့ရဲ့ Unit Function မှာဆိုလဲ အတူတူပါပဲ။

Unit Functionဆိုတာ Result ကို ပြန်မယူတဲ့ Functionမျိုးကိုဆိုလိုတာ။

ဥပမာ -

ဒါကတော့ print ထုတ်ပေးတဲ့ Functionလေးပါ။

အပေါ်က Unit က Typeအနေနဲ့ သုံးလိုက်တာ အောက်က returnနားက Unit‌ကြတော့ value အနေနဲ့ပေါ့။

Kotlin မှာ Unit Return Type နဲ့ Value ကို မထည့်လဲရတယ် -

သူက Java ကနေတဆင့် သ‌န္ဓေပြောင်းလာတာဆိုတော့ Kotlin Unit Function က Java ရဲ့ void Function နဲ့အတူတူပါပဲ..

ဒါမဲ့ Kotlinသည် Java (JVM) အပြင် JavaScript (JS)နဲ့ Native တွေအတွက်ရော ရေးလို့ရတယ်ဆိုတာတော့ သတိချပ်စေချင်ပါတယ်။

(Static အပြင် Dynamic Type ရောရလို့ပါ)

4. Inferred Return Type

Inferred ဆိုခါမှ valနဲvarတုန်းက တခါပြောခဲ့ဖူးတဲ့ Inferred Typeအကြောင်း‌‌လေး အစပြန်ဖော်ရဦးမယ်ဗျာ..

Kotlin မှာက Assign (=)လုပ်တဲ့အခါတိုင်း Type မထည့်လဲရတယ်‌ဆိုတာလေး

var cup:String = “Coffee”
var cup =”Coffee”

နောက်က “Coffee” သည် String ဖြစ်တယ်ဆိုတာ Kotlinမှာ အလိုလိုသိတယ်။

Kotlin Function ရဲ့ Body မှာ Code Line တစ်ကြောင်းထဲရှိတဲ့ အခါမျိုးမှာအဲ့လို Inferred Return Type ကို သုံးလို့ရတယ်။

အရင်ပုံစံ‌ဟောင်းက -

အဲ့မှာက return တစ်ကြောင်းတည်းရှိတော့။ Block Scope “{}”ကိုဖြုတ်ပြီး။

ဒီလိုလေး ရေးလို့ရတယ်-

fun makeCoffee (): String = “Coffee”

အပေါ်က Functionနဲ့ အတူတူပါပဲ။

Kotlinမှာ Inferred Type ပေးထားတော့ Type ကို အလိုလိုသိတော့။

နောက်ထပ်ပြင်လို့ရသေးတယ် -

fun makeCoffee ()= “Coffee”

ဒါဆို Codeက ကျစ်ကျစ်လစ်လစ်ဖြစ်သွားပြီ။

ဒါနဲ့ ဘာလို့ ချုံ့ရေးရတာလဲ?
မရေးလဲရလားဆို … ရတယ်ဗျ ချုံ့မရေးလဲ..

ချုံ့ရေးတဲ့အခါမှာကြ Codeဖတ်ရတာ ပိုမြန်တယ်။ ကြည့်တဲ့သူအနေနဲ့ နားလည်လွယ်တယ်။ တနည်းအားဖြင့် Codeရဲ့ Readability ပိုကောင်းသွားတယ်လို့ ဆိုနိုင်ပါတယ်။

4. Local Functions

သူကတော့ ရှင်းပါတယ်။ Function အတွင်း ထဲမှာ Function နောက်တစ်ခုထပ်ရှိနေတာမျိုးး

Local ဆိုတဲ့အတိုင်း သူ့ကိုဆောက်ထားတဲ့ Function ထဲကနေ ပဲ ခေါ်ခွင့်ရှိတယ်။

ခုနက local() ဆိုတဲ့ Function လေးကို main() အတွင်းမှာပဲ ခေါ် သုံးခွင့်ရှိတယ်။

သူ့ကို သုံးကြတာ သိပ်မတွေ့မိဘူး။ ကျွန်တော်လဲ မသုံးဖြစ်ဘူး။

ရှေ့မှာပြောခဲ့တဲ့ Kotlin Function ရဲ့ Feature တွေအားလုံးက မဖြစ်မနေ ထည့်သုံးရမယ်လို့တော့ မဆိုလိုပါဘူး။ သူ့တို့က Kotlin Codeတွေကို ကျစ်လစ်စေရုံမျှသာ။
ဆိုတော့ အဆင်ပြေသလို သုံးလို့ရပါတယ် . . .

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Aung Ye Htet
Aung Ye Htet

Written by Aung Ye Htet

Android Developer, Kotlin Enthusiast

No responses yet

Write a response