Thuta Learning
Kotlin
AdvancedProgrammingအခြေခံ

Generics & Extension Functions

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

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

  • <T> နဲ့ generic function ဒါမှမဟုတ် class ကို ကြေညာပြီး type ကို Kotlin က infer လုပ်ခိုင်းနိုင်ဖို့
  • T : Comparable<T> လို bound နဲ့ type parameter ကို ကန့်သတ်ပြီး bound က body ထဲမှာ ဘာကို ခွင့်ပြုလဲ ပြောနိုင်ဖို့
  • Extension function ရေးပြီး ဘာကြောင့် runtime type မဟုတ်ဘဲ ကြေညာထားတဲ့ type အလိုက် ရွေးလဲ ရှင်းပြနိုင်ဖို့
  • List<String> ကို List<Any> အဖြစ် သုံးလို့ရပေမဲ့ MutableList<String> ကို MutableList<Any> အဖြစ် ဘာကြောင့် မသုံးရလဲ ပြောနိုင်ဖို့

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

Generics နဲ့ extension ကို ရိုးရိုးလေး ပြောရရင်

Generics ကြောင့် code တစ်ခုတည်းက type အများကြီးအတွက် အလုပ်လုပ်ပြီး ဆက်ပြီး စစ်ဆေးမှု ခံရပါတယ်: Stack<String> နဲ့ Stack<Int> က class တစ်ခုတည်းကို မျှသုံးပေမဲ့ ပထမ stack ထဲကို Int ကို ဘယ်တော့မှ push လုပ်လို့ မရပါဘူး။ Extension function တွေကတော့ ရှိပြီးသား type တစ်ခုကို၊ ကိုယ်မရေးခဲ့တဲ့ type ကိုတောင် function အသစ်တစ်ခု ထည့်ပေးပြီး member တစ်ခုလို 'Ada Lovelace'.initials() လို့ ခေါ်ခွင့်ပေးပါတယ်။

Generic ကို label ကပ်ထားတဲ့ သေတ္တာတစ်လုံးလို မြင်ကြည့်ပါ: သေတ္တာဒီဇိုင်းက တူပေမဲ့ တစ်လုံးစီမှာ ဘာထည့်ထားလဲ ပြောတဲ့ label ပါပြီး ထည့်တိုင်း ထုတ်တိုင်း အဲဒီ label ကို စစ်ပါတယ်။ Extension ကတော့ တခြားသူ သေတ္တာပေါ်ကို ကပ်လိုက်တဲ့ ညွှန်ကြားချက်ပါတဲ့ sticker ပါ: သေတ္တာကိုယ်တိုင်ကို မပြောင်းပါဘူး၊ အဲဒီအမျိုးအစား သေတ္တာကို မြင်တိုင်း sticker ကို ဖတ်မယ်လို့ သဘောတူလိုက်တာပါ။

Problem → Why → Solution

ပြဿနာ: type တစ်ခုစီအတွက် helper ကူးရခြင်း

Int list တစ်ခုထဲက အကြီးဆုံးတန်ဖိုးကို ရှာတဲ့ helper တစ်ခု ရေးပါတယ်။ တစ်ပတ်ကြာတော့ String အတွက် လိုလာပြီး ပြီးရင် date အတွက်ပါ လိုလာပါတယ်။ သုံးကြိမ် ကူးထားတာ အလုပ်လုပ်ပေမဲ့ bug fix တစ်ခုကို သုံးနေရာ ပြင်ရတဲ့အထိပါ။ အတိုလမ်းဖြစ်တဲ့ Any list ကတော့ type ကို ဆုံးရှုံးစေပါတယ်: ရလဒ်က Any ဖြစ်လို့ ပြန် cast လုပ်ရပြီး String list ထဲကို Double ဝင်သွားတာကိုလည်း ဘာမှ မတားပါဘူး။

ဒုတိယပြဿနာက utility code ပါ။ Utils ဖိုင်ထဲမှာ fun initialsOf(name: String) ရေးထားတဲ့အတွက် ခေါ်တိုင်း initialsOf(user.name) ဖြစ်နေပြီး user.name.initials() နဲ့ယှဉ်ရင် ပြောင်းပြန်ဖတ်ရပါတယ်။ String က standard library ပိုင်ဆိုင်ပြီး member အသစ်ထည့်ဖို့ ပြင်လို့ မရပါဘူး။

copy-per-type.kt
fun largestInt(values: List<Int>): Int? = values.maxOrNull()

fun largestString(values: List<String>): String? = values.maxOrNull()

// "works for everything", but the answer comes back as Any
fun largestAny(values: List<Any>): Any? = values.firstOrNull()

object Utils {
    fun initialsOf(name: String) = name.split(" ").map { it.first() }.joinToString("")
}

fun main() {
    println(largestInt(listOf(1, 5)))
    println(largestAny(listOf(1, "a")))
    println(Utils.initialsOf("Ada Lovelace"))
}

Type parameter နဲ့ extension က ဘာကို ဖြေရှင်းပေးလဲ

Type parameter တွေက ဝင်တဲ့အရာနဲ့ ထွက်တဲ့အရာကြား ဆက်သွယ်ချက်ကို ထိန်းပေးတဲ့အတွက် အဓိပ္ပာယ်ဖွင့်ဆိုချက် တစ်ခုက type အားလုံးအတွက် လုံလောက်ပြီး compiler က သုံးတဲ့နေရာတိုင်းကို ဆက်စစ်ပါတယ်။ Extension function တွေကတော့ မူလ class ကို မထိဘဲ ဒုတိယပြဿနာကို ဖြေရှင်းပေးပါတယ်: dot ရဲ့ ရှေ့မှာ ရေးတဲ့ ပထမ parameter ပါတဲ့ ရိုးရိုး function တွေမို့ code ကို ဘယ်ကနေညာ ဖတ်လို့ရပြီး IDE completion ကလည်း dot နောက်မှာ ရှာတွေ့ပါတယ်။

Core concept: type parameter၊ bound နဲ့ extension

Type parameter ဆိုတာ angle bracket ထဲမှာ ကြေညာတဲ့ type အတွက် နေရာယူစာလုံးပါ: fun <T> firstOrNull(list: List<T>): T? ဒါမှမဟုတ် class Stack<T> ပါ။ ကြေညာချက်ထဲက T တိုင်းက ခေါ်သူရွေးတဲ့ type တစ်ခုတည်းကို ကိုယ်စားပြုပါတယ်။ များသောအားဖြင့် Kotlin က argument တွေကနေ infer လုပ်ပေးလို့ largest(listOf(3, 9, 4)) မှာ <Int> ရေးစရာမလိုပါဘူး၊ Stack<String>() မှာတော့ infer လုပ်စရာ မရှိလို့ ရေးပေးရပါတယ်။ Type parameter က compile time မှာပဲ ရှိပါတယ်: run time မှာ List<String> နဲ့ List<Int> နှစ်ခုလုံး List ပဲဖြစ်ပါတယ်၊ ဒါကို type erasure လို့ ခေါ်ပြီး ရိုးရိုး T အတွက် it is T ကို compiler က ပယ်တာ အဲဒါကြောင့်ပါ။

Bound မရှိတဲ့ T က ဘာမဆို ဖြစ်နိုင်လို့ function body က type အားလုံးအတွက် အလုပ်လုပ်တာတွေကိုပဲ လုပ်ခွင့်ရှိပါတယ်။ ပိုလုပ်ချင်ရင် upper bound ထည့်ပါ: <T : Comparable<T>> က 'တခြား T နဲ့ နှိုင်းယှဉ်လို့ရတဲ့ ဘယ် T မဆို' လို့ ပြောတာပါ။ အဲဒီအခါ body ထဲမှာ value > best ကို ရေးလို့ရသွားပြီး ခေါ်သူက Int၊ String ဒါမှမဟုတ် Comparable type ဘာမဆို ပေးနိုင်ပေမဲ့ Any ကို မပေးနိုင်ပါဘူး။ Bound မပါရင် တူညီတဲ့ code က နှိုင်းယှဉ်တဲ့နေရာမှာ 'unresolved reference' ဆိုပြီး fail ဖြစ်ပါတယ်။ Type တစ်ခုမှာ bound အများကြီး လိုရင် where clause သုံးပါ: where T : CharSequence, T : Comparable<T>။

Extension function ကို နာမည်ရှေ့မှာ receiver type နဲ့ ကြေညာပါတယ်: fun String.initials(): String။ Body ထဲမှာ this က receiver ဖြစ်ပြီး member တွေကို prefix မပါဘဲ သုံးလို့ရပေမဲ့ public member တွေပဲ ရပါတယ်၊ extension က private member ကို မထိနိုင်ပါဘူး။ ခေါ်ပုံက member call နဲ့ တူပေမဲ့ ("Ada Lovelace".initials()) String ထဲကို ဘာမှ ထည့်မထားပါဘူး။ Compiler က static function call အဖြစ် ပြောင်းပြီး receiver ကို ပထမ argument အဖြစ် ပေးပါတယ်။ ဒါကြောင့် extension က nullable type (fun String?.orPlaceholder()) နဲ့ ကိုယ်မပြင်နိုင်တဲ့ class တွေပေါ်မှာ အလုပ်လုပ်တာပါ။

Featureပုံစံရတာ
Generic functionfun <T> name(x: T): Ttype အများကြီးအတွက် definition တစ်ခု
Upper bound<T : Comparable<T>>body ထဲမှာ နှိုင်းယှဉ်ခွင့်
Generic classclass Stack<T>container တစ်ခု၊ element type အများကြီး
Extension functionfun String.initials()ဘယ် type ပေါ်မှာမဆို member-style call
Nullable receiverfun String?.orPlaceholder()null ပေါ်မှာလည်း ခေါ်လို့ လုံခြုံ

ခေါ်တာကို compiler က ဆုံးဖြတ်တာမို့ ဘယ် extension ပြေးမလဲဆိုတာ object ရဲ့ runtime type အပေါ် မမူတည်ဘဲ expression ရဲ့ ကြေညာထားတဲ့ type အပေါ် မူတည်ပါတယ်။ Shape နဲ့ Circle နှစ်ခုလုံးမှာ describe ဆိုတဲ့ extension ရှိရင် Shape အဖြစ် ကြေညာထားတဲ့ variable က Circle ကိုင်ထားရင်တောင် Shape ရဲ့ extension ကိုပဲ အမြဲ run ပါတယ်။ Signature တူတဲ့ member function ကတော့ extension ကို အမြဲ အနိုင်ရပါတယ်၊ ဒါကြောင့် extension က ရှိပြီးသား အပြုအမူကို ဘယ်တော့မှ မပြောင်းနိုင်ပါဘူး၊ compiler က extension ကို member က ဖုံးထားတယ်လို့ သတိပေးပါတယ်။

Generics က ကွက်လပ်ဖြည့်၊ extension က စာလုံးတိုတို
  1. T က ခေါ်သူ ဖြည့်မယ့် ကွက်လပ်
  2. Bound က T ဘာလုပ်နိုင်ရမလဲ ပြောတယ်
  3. Extension က static function တစ်ခု
  4. ကြေညာထားတဲ့ type က extension ရွေးတယ်

Generic body ထဲမှာ 'unresolved reference' ပေါ်ရင် ပုံမှန် ဖြေရှင်းနည်းက T ပေါ်မှာ bound ထည့်တာပါ။

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

kotlin
fun <T> name(param: T): T { ... }

fun <T : Comparable<T>> name(items: List<T>): T? { ... }

class Name<T>(val value: T) { ... }

fun ReceiverType.name(param: Type): Result { ... this ... }

fun <T> Name<T>.name(): List<T> { ... }

T ကို function ရဲ့ ရှေ့မှာ ကြေညာပြီး parameter နဲ့ return type ထဲမှာ သုံးပါ၊ body က T ကနေ နှိုင်းယှဉ်တာ၊ ပေါင်းတာလို အရာကို လိုအပ်မှ bound ထည့်ပါ။ Extension မှာ receiver type ကို dot ရဲ့ ရှေ့ ရေးပြီး body ထဲမှာ this နဲ့ သုံးပါ။

Worked example: Stack<T>၊ bounded function နဲ့ extension

အောက်က program ထဲမှာ ပိုင်ဆိုင်တဲ့ type သုံးမျိုး ပါပါတယ်: generic class (Stack<T>)၊ bound ပါတဲ့ generic function (largest) နဲ့ extension နှစ်ခု (String ပေါ်မှာ တစ်ခု၊ ကိုယ့် class ပေါ်မှာ generic တစ်ခု)။ Output မထုတ်ခင် ဘာ print ထွက်မလဲ ကြိုတွက်ပါ၊ အထူးသဖြင့် size = ... ထဲက တန်ဖိုးကို။

generics-extensions.kt
class Stack<T> {
    private val items = mutableListOf<T>()

    fun push(item: T) {
        items.add(item)
    }

    fun pop(): T? = items.removeLastOrNull()

    val size: Int
        get() = items.size
}

fun <T : Comparable<T>> largest(values: List<T>): T? {
    var best: T? = null
    for (value in values) {
        if (best == null || value > best) best = value
    }
    return best
}

fun String.initials(): String =
    split(" ")
        .filter { it.isNotBlank() }
        .map { it.first().uppercaseChar() }
        .joinToString("")

fun <T> Stack<T>.pushAll(vararg values: T) {
    for (value in values) push(value)
}

fun main() {
    val words = Stack<String>()
    words.pushAll("kotlin", "generics", "extensions")
    println("size = ${words.size}, top = ${words.pop()}")

    println(largest(listOf(3, 9, 4)))
    println(largest(listOf("pear", "apple", "zebra")))
    println(largest(emptyList<Double>()))

    println("Ada Lovelace".initials())
}

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

  1. A generic classclass Stack<T> {

    T ကို class ပေါ်မှာ ကြေညာထားလို့ Stack<String>() နဲ့ Stack<Int>() တို့က class တစ်ခုတည်းကို မျှသုံးပေမဲ့ T တိုင်းက အဲဒီ stack အတွက် ရွေးထားတဲ့ type တစ်ခုတည်း ဖြစ်ပါတယ်။ push(item: T) က တခြား type ကို မလက်ခံပါဘူး။

  2. T? for an empty stackfun pop(): T? = items.removeLastOrNull()

    Stack အလွတ်မှာ ထိပ်ဆုံး element မရှိတဲ့အတွက် return type က T? ဖြစ်ပါတယ်။ removeLastOrNull က list အလွတ်မှာ exception မပစ်ဘဲ null ပြန်ပေးပါတယ်၊ ခေါ်သူက null ကို ကိုင်တွယ်ရမယ်ဆိုတာ type ကပဲ ပြောပြပါတယ်။

  3. An upper boundfun <T : Comparable<T>> largest(values: List<T>): T? {

    T : Comparable<T> က T ကို တခြား T တစ်ခုနဲ့ နှိုင်းယှဉ်လို့ရတဲ့ type အဖြစ် ကန့်သတ်ပါတယ်။ ဒါကြောင့် Int နဲ့ String ကို ခေါ်လို့ရပေမဲ့ Any list ကို compiler က ငြင်းပါမယ်။ Return type T? က list အလွတ်အတွက် null ကို ခွင့်ပြုပါတယ်။

  4. Comparing with the boundif (best == null || value > best) best = value

    value > best က compareTo ကို ခေါ်တာဖြစ်ပြီး bound ရှိလို့သာ ခွင့်ပြုတာပါ။ best == null || ရဲ့ နောက်မှာ best က null မဟုတ်တော့ဘူးဆိုတာ compiler က သိတဲ့အတွက် (smart cast) နှိုင်းယှဉ်ရာမှာ ?. မလိုပါဘူး။

  5. An extension on Stringfun String.initials(): String =

    String က receiver ဖြစ်လို့ body ထဲက split(" ") ဆိုတာ this.split(" ") ပါပဲ။ ခေါ်ပုံက "Ada Lovelace".initials() ဖြစ်ပြီး ရလဒ် AL ပါ။ String class ကို ပြင်လိုက်တာ မဟုတ်ပါဘူး။

  6. A generic extensionfun <T> Stack<T>.pushAll(vararg values: T) {

    Extension ကလည်း generic ဖြစ်နိုင်ပါတယ်: T က receiver Stack<T> နဲ့ vararg values နှစ်ခုလုံးမှာ တူညီတဲ့ type ဖြစ်ပါတယ်။ Body က public push ကိုပဲ သုံးတာမို့ private items ကို မထိပါဘူး။ ပထမ println မှာ size = 3 မြင်ရတာက string template ကို ဘယ်ကနေညာ တွက်လို့ pop မလုပ်ခင် size ကို ဖတ်လို့ပါ။

You should see
size = 3, top = extensions
9
zebra
null
AL

Extension တွေကို static ရွေးတယ်

အောက်ကနမူနာက အချက်နှစ်ချက်ကို ပြပါတယ်။ ပထမ၊ val shape: Shape = Circle() လို့ ကြေညာထားရင် shape.describe() က Circle ရဲ့ extension ကို မခေါ်ဘဲ Shape ရဲ့ extension ကို ခေါ်ပါတယ်၊ Circle() ကို တိုက်ရိုက် ခေါ်မှ Circle ရဲ့ extension ပြေးပါတယ်။ ဒုတိယ၊ Greeter ရဲ့ member function hello က တူညီတဲ့ extension ကို အနိုင်ယူပါတယ်။

static-resolution.kt
open class Shape
class Circle : Shape()

fun Shape.describe() = "some shape"
fun Circle.describe() = "a circle"

class Greeter {
    fun hello() = "member hello"
}

fun Greeter.hello() = "extension hello"

fun main() {
    val shape: Shape = Circle()
    println(shape.describe())
    println(Circle().describe())
    println(Greeter().hello())
}

Output က some shape၊ a circle၊ member hello ပါ။ Compile လုပ်တုန်း compiler က 'this extension is shadowed by a member' လို့ warning ပေးပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ extension ကို ဘယ်တော့မှ မခေါ်နိုင်လို့ပါ။ Runtime type အလိုက် အပြုအမူ ကွဲစေချင်ရင် extension မဟုတ်ဘဲ open member function ဒါမှမဟုတ် interface ကို သုံးပါ။

Variance နှစ်မိနစ်: out နဲ့ in

String တစ်ခုက Any တစ်ခုဖြစ်ပေမဲ့ List<String> က List<Any> ဖြစ်မလား ဆိုတာ မေးခွန်းပါ။ အဖြေက List က item တွေကို ထုတ်ပေးတာပဲ လုပ်လို့ ဖြစ်ပါတယ် (List<out E>)၊ MutableList ကတော့ item ထည့်လို့ရတာမို့ မဖြစ်ပါဘူး။ MutableList<String> ကို MutableList<Any> အဖြစ် လက်ခံခွင့်ရှိရင် String list ထဲကို Int ထည့်လို့ ရသွားမှာပါ။

ကြေညာချက်အဓိပ္ပာယ်ဥပမာ
interface Source<out T>T ကို ထုတ်ပေးတာပဲ လုပ်နိုင်၊ producerList<out E>
interface Sink<in T>T ကို လက်ခံတာပဲ လုပ်နိုင်၊ consumerComparable<in T>
class Box<T>invariant: ထုတ်လည်းရ ထည့်လည်းရMutableList<E>
variance.kt
interface Source<out T> {
    fun next(): T
}

interface Sink<in T> {
    fun accept(item: T)
}

fun main() {
    val names: List<String> = listOf("Ada", "Linus")
    val things: List<Any> = names
    println(things)

    val editable: MutableList<String> = mutableListOf("Ada")
    // val mixed: MutableList<Any> = editable   // error: MutableList is invariant
    // mixed.add(42)                            // would put an Int among the Strings
    editable.add("Linus")
    println(editable)

    val source: Source<String> = object : Source<String> {
        override fun next() = "hello"
    }
    val anySource: Source<Any> = source
    println(anySource.next())

    val printer: Sink<Any> = object : Sink<Any> {
        override fun accept(item: Any) = println("got $item")
    }
    val stringSink: Sink<String> = printer
    stringSink.accept("text")
}

Comment ဖြုတ်ပြီး compile လုပ်ကြည့်ရင် compiler က initializer type mismatch: expected 'MutableList<Any>', actual 'MutableList<String>' လို့ ပြောပါလိမ့်မယ်။ အကျဉ်းချုပ်ကတော့ producer အတွက် out၊ consumer အတွက် in ဖြစ်ပြီး ကိုယ်ပိုင် class အများစုမှာ ဘာမှ ထည့်စရာ မလိုပါဘူး။ ဒါပေမဲ့ List<String> ကို List<Any> parameter ထဲ ပေးလို့ရတာ ဘာကြောင့်လဲ ဆိုတာ နားလည်ထားရင် error message တွေကို ဖတ်ရ လွယ်ပါတယ်။

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

လက်တွေ့ project တွေမှာ generics နဲ့ extension ကို ဘယ်မှာ တွေ့ရလဲ

Standard library တစ်ခုလုံးက generics နဲ့ extension တွေနဲ့ ဆောက်ထားပါတယ်။ List<T>၊ Map<K, V>၊ Result<T> နဲ့ Sequence<T> တို့က generic type တွေဖြစ်ပြီး map၊ filter နဲ့ joinToString တို့က Iterable<T> ပေါ်က extension function တွေမို့ collection ဘာမဆိုပေါ်မှာ ခေါ်လို့ရတာပါ။ Application code မှာတော့ Repository<T> ဒါမှမဟုတ် generic ApiResult<T> က ထပ်တလဲလဲ ရေးရတဲ့ ပိုက်လိုင်းကို ဖုံးကွယ်ပေးပြီး String.toSlug() ဒါမှမဟုတ် Double.toMoney() လို extension တွေက ထပ်ခါထပ်ခါ ပြောင်းရတာတွေကို ဖတ်ရလွယ်တဲ့ call တွေ ဖြစ်စေပါတယ်။ Android KTX လို library တွေက framework class တွေပေါ်မှာ extension function တွေ ထည့်ပေးပါတယ်။

Extension တွေကို သုံးတဲ့ file ဒါမှမဟုတ် module အနီးမှာ ထားပါ (private fun ဆို ပိုကောင်းပါတယ်) ဒါမှ တခြား library နဲ့ နာမည်မတိုက်ပါဘူး။ အရာအားလုံးကို generic မလုပ်ပါနဲ့: concrete type တစ်ခုတည်းကိုပဲ သုံးမယ်ဆိုရင် ရိုးရိုး type က ပိုရှင်းပါတယ်။

အမှားများ

Generics နဲ့ extension မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

Extension ကို override လို သဘောထားခြင်း

Shape အတွက် extension တစ်ခုနဲ့ Circle အတွက် extension တစ်ခု ရေးပြီး Shape variable ထဲ Circle ထည့်ရင် Shape ရဲ့ extension ပဲ ပြေးပါတယ်၊ ဒါ polymorphism မဟုတ်ပါဘူး။ Runtime type အလိုက် အပြုအမူ လိုရင် member function (open နဲ့ override) ဒါမှမဟုတ် interface ကို သုံးပါ၊ extension ကို helper တွေအတွက်ပဲ ထားပါ။

Bound မရှိတဲ့ T ကို နှိုင်းယှဉ်ခြင်း

fun <T> biggest(a: T, b: T) = if (a > b) a else b က compile မဖြစ်ပါဘူး၊ compiler က 'unresolved reference. None of the following candidates is applicable because of a receiver type mismatch' လို့ ပြောပါတယ်၊ T က နှိုင်းယှဉ်လို့မရတဲ့ type ဖြစ်နိုင်လို့ပါ။ <T : Comparable<T>> ထည့်ပါ၊ cast ဒါမှမဟုတ် Any ကို သုံးပြီး ဖာထေးမနေပါနဲ့။

Extension က private member ကို ထိနိုင်မယ်လို့ ထင်ခြင်း

class Account(private val pin: String) အတွက် fun Account.check() = pin.length က cannot access 'val pin: String': it is private in '/Account' ဆိုတဲ့ error နဲ့ fail ဖြစ်ပါတယ်၊ extension က class အပြင်က function သက်သက်မို့ပါ။ Private state လိုတဲ့ logic ကို class ထဲမှာပဲ ထားပါ၊ ဒါမှမဟုတ် လိုအပ်တာကို public member အဖြစ် ဖော်ပြပါ။

Best Practices

Best practices

  • Body က လိုမှ type parameter မှာ bound ထည့်ပါ၊ parameter များရင် K နဲ့ V၊ TItem လို အခန်းကဏ္ဍအလိုက် နာမည်ပေးပါ။
  • သေးငယ်ပြီး state မရှိတဲ့ helper တွေကို extension အဖြစ် ရေးပါ၊ private state လိုတဲ့ အပြုအမူကို class ကိုယ်တိုင်ထဲမှာ ထားပါ။
  • Extension ကို သုံးတဲ့ file ဒါမှမဟုတ် module ထဲမှာ ကြေညာပါ (private fun) ဒါမှ တခြား library နဲ့ မတိုက်ပါဘူး။
  • Member က အမြဲအနိုင်ရတာမို့ type မှာ နာမည်တူ method ရှိပြီးသားဆိုရင် extension ကို နာမည်တခြား ပေးပါ။

Mini challenge

Mini challenge: Box၊ isSortedAscending နဲ့ toTitleCase

Tool သေးသေးလေး သုံးခု ထည့်ပါ: value တစ်ခု ကိုင်ထားပြီး map function က Box<R> ပြန်ပေးတဲ့ generic class Box<T>၊ List ပေါ်က extension ဖြစ်ပြီး T က Comparable ဖြစ်တဲ့ list က ငယ်စဉ်ကြီးလိုက် စီထားလား ဆိုတာ ပြောတဲ့ isSortedAscending၊ ပြီးတော့ 'hELLO kotlin' ကို 'Hello Kotlin' အဖြစ် ပြောင်းတဲ့ String ပေါ်က extension toTitleCase ပါ။

  • Box<T> ကို val value နဲ့ fun <R> map(transform: (T) -> R): Box<R> ပါအောင် ရေးရမယ်
  • isSortedAscending ကို T : Comparable<T> bound နဲ့ List<T> ပေါ်က extension အဖြစ် ရေးပြီး list အလွတ်အတွက် true ပြန်ရမယ်
  • toTitleCase က စာလုံးကြားက space အပိုတွေကို ကိုင်တွယ်ပြီး စာလုံးတစ်လုံးစီကို အစကြီးအဖြစ် ပြောင်းရမယ်
  • Box(21).map { it * 2 }.map { "answer: $it" } ကို print ထုတ်ပြီး ကျန်တဲ့ function နှစ်ခုကိုလည်း ခေါ်ကြည့်ရမယ်
tools.kt
// TODO: a generic class that holds a value; map returns a Box of the new type
class Box<T>(val value: T) {
    // TODO: fun <R> map(transform: (T) -> R): Box<R>
}

// TODO: make this generic with a bound so any Comparable element type works
fun List<Int>.isSortedAscending(): Boolean {
    TODO()
}

// TODO: lower-case every word, then upper-case its first letter
fun String.toTitleCase(): String {
    TODO()
}

fun main() {
    // TODO: build Box(21), map to double it, then map to "answer: <value>", print value
    // TODO: print isSortedAscending for a sorted Int list, an unsorted String list
    //       and an empty list
    // TODO: print "  hELLO   kotlin wORLD ".toTitleCase()
}

ရလဒ်: answer: 42၊ ပြီးရင် true၊ false၊ true ကို print ထုတ်ပြီး နောက်ဆုံးမှာ Hello Kotlin World ကို print ထုတ်ပါတယ်။

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

Box.map ထဲမှာ Box(transform(value)) ကို return လုပ်ပါ။ isSortedAscending အတွက် index 1 ကနေ size အထိ loop လုပ်ပြီး this[i - 1] > this[i] ဖြစ်ရင် false ပြန်ပါ။ toTitleCase အတွက် split(" ") ကို filter { it.isNotEmpty() } နဲ့ ဆက်ပြီး joinToString(" ") သုံးပါ။

အဖြေ ကြည့်ရန်
tools.kt
class Box<T>(val value: T) {
    fun <R> map(transform: (T) -> R): Box<R> = Box(transform(value))
}

fun <T : Comparable<T>> List<T>.isSortedAscending(): Boolean {
    for (i in 1 until size) {
        if (this[i - 1] > this[i]) return false
    }
    return true
}

fun String.toTitleCase(): String =
    split(" ")
        .filter { it.isNotEmpty() }
        .joinToString(" ") { word ->
            word.lowercase().replaceFirstChar { it.uppercase() }
        }

fun main() {
    val box = Box(21).map { it * 2 }.map { "answer: $it" }
    println(box.value)

    println(listOf(1, 2, 2, 5).isSortedAscending())
    println(listOf("b", "a").isSortedAscending())
    println(emptyList<Int>().isSortedAscending())

    println("  hELLO   kotlin wORLD ".toTitleCase())
}

Quick Quiz 1 / 3

open class Shape၊ class Circle : Shape() ရှိပြီး fun Shape.describe() = "shape" နဲ့ fun Circle.describe() = "circle" ရေးထားပါတယ်။ val s: Shape = Circle() ရေးပြီး println(s.describe()) လုပ်ရင် ဘာထွက်မလဲ?

Quick Quiz 2 / 3

Body ထဲမှာ value > best ကို ရေးခွင့်ရအောင် ဘယ် ကြေညာချက်က လိုအပ်လဲ?

Quick Quiz 3 / 3

val names: MutableList<String> နဲ့ val readOnly: List<String> ရှိပါတယ်။ အောက်က ဘယ် line ကို compile လုပ်လို့ရလဲ?

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

  • <T> က ခေါ်သူက ဖြည့်တဲ့ type ကွက်လပ်ဖြစ်ပြီး Kotlin က များသောအားဖြင့် infer လုပ်ပေးပါတယ်၊ run time မှာတော့ erase ဖြစ်သွားပါတယ်။
  • T : Comparable<T> လို bound က body ထဲမှာ T ပေါ်မှာ ဘာလုပ်လို့ရလဲ ဆိုတာကို ခွင့်ပြုပေးပြီး မရှိရင် unresolved reference ဖြစ်ပါတယ်။
  • Extension function က static function ဖြစ်ပြီး ကြေညာထားတဲ့ type အလိုက် ရွေးကာ member ကို ဘယ်တော့မှ မအနိုင်ယူနိုင်ပါဘူး၊ private member ကိုလည်း မထိနိုင်ပါဘူး။
  • List<out E> ကို List<Any> အဖြစ် သုံးလို့ရပေမဲ့ MutableList က invariant ဖြစ်လို့ MutableList<String> ကို MutableList<Any> အဖြစ် မသုံးနိုင်ပါဘူး။

Kotlin language guide — Kotlin

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

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

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

Generics & Extension Functions | Thuta Learning