ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Data class ဆိုတာ data သယ်ဆောင်ရတာကိုပဲ အလုပ်အဓိကထားတဲ့ class ပါ — အညွှန်းကပ်ထားတဲ့ ဘူးတစ်ခု ဒါမှမဟုတ် table တစ်ခုရဲ့ row တစ်ကြောင်းလိုပါပဲ။ Field တွေကိုပဲ စာရင်းပြုပြီး ပျင်းစရာ method တွေကို Kotlin က ရေးပေးပါတယ်: print ထုတ်ပုံ၊ ဘူးနှစ်လုံးကို နှိုင်းယှဉ်ပုံ၊ ပြင်ထားတဲ့ copy ဆောက်ပုံတို့ပါ။ ဒီ method တွေက အကြောင်းအရာကို နှိုင်းယှဉ်ပြီး print ထုတ်တာမို့ အညွှန်းနဲ့ တန်ဖိုး တူတဲ့ ဘူးနှစ်လုံးက တန်းတူ ဖြစ်ပါတယ်။
Data class အတွက် ကုန်ကျစရိတ်က class ရှေ့က data ဆိုတဲ့ စကားလုံးတစ်လုံးပါပဲ။ User, API response, database row နဲ့ UI state တွေက သဘာဝအတိုင်း data class ပါ။
Problem → Why → Solution
ပြဿနာ: တန်ဖိုးတူပေမယ့် မတန်းတူတဲ့ object တွေ
ပုံမှန် class User(val name: String, val age: Int) ကို ယူပြီး တန်ဖိုးတူတဲ့ user နှစ်ယောက် ဖန်တီးကြည့်ပါ။ == နဲ့ နှိုင်းယှဉ်ရင် false ရပါတယ် — ပုံမှန် class မှာ == က "တူတဲ့ object" ကို ဆိုလိုတာဖြစ်ပြီး "တူတဲ့ အကြောင်းအရာ" ကို မဆိုလိုပါဘူး။ တစ်ခုကို print ထုတ်ရင် User@ နောက်မှာ hex နံပါတ်တစ်ခု လိုမျိုး ပေါ်ပြီး ဘာမှ မပြောပြပါဘူး၊ တစ်ခုကို HashSet ထဲ ထည့်ရင် နောက် copy ကို ရှာမတွေ့ပါဘူး။ လက်နဲ့ ပြင်ဖို့ဆိုရင် equals, hashCode, toString နဲ့ copy function ကို ရေးရပြီး hashCode မေ့တာက classic bug တစ်ခုပါ။
class User(val name: String, val age: Int)
fun main() {
val a = User("Alice", 28)
val b = User("Alice", 28)
println(a == b) // false: compares identity, not contents
println(a) // User@ followed by a hex number
val seen = hashSetOf(a)
println(b in seen) // false: the set cannot recognise the twin
}လက်နဲ့ရေးတဲ့ boilerplate က ဘာကြောင့် အန္တရာယ်ရှိလဲ
ဒီ method တွေကို လက်နဲ့ရေးတာ ထပ်ခါထပ်ခါ ဖြစ်ပြီး မသိမသာ အမှားလေး ဝင်လာလွယ်ပါတယ်: field တစ်ခု ပြောင်းပြီး equals ကို မပြင်မိရင် object နှစ်ခု မှန်မှန်ကန်ကန် နှိုင်းယှဉ်လို့ မရတော့ပါဘူး။ Data class က primary constructor ကနေ အားလုံးကို ထုတ်ပေးလို့ field တွေနဲ့ ကွဲလွဲမသွားနိုင်ပါဘူး။ data keyword ရှိရတာ ဒီအတွက်ပါ — ဒီ class ဟာ သူ့ရဲ့ အကြောင်းအရာပဲ ဖြစ်တယ်လို့ ကြေညာတာပါ။
Core concept: identity မဟုတ်ဘဲ value
Class ကို data လို့ မှတ်လိုက်ရင် compiler က primary constructor ထဲက property တွေကနေ အရာငါးခု ထုတ်ပေးပါတယ်: အကြောင်းအရာနဲ့ နှိုင်းယှဉ်တဲ့ equals() နဲ့ hashCode()၊ User(name=Alice, age=28) လို print ထုတ်တဲ့ toString()၊ ပြောင်းထားတဲ့ copy ဆောက်တဲ့ copy()၊ destructuring ကို ဖြစ်စေတဲ့ componentN() function တွေပါ။ လိုအပ်ချက်တွေက သေးပါတယ်: primary constructor မှာ parameter တစ်ခုအနည်းဆုံး ရှိရပြီး parameter တိုင်းက val ဒါမှမဟုတ် var ဖြစ်ရပါမယ်။ မဟုတ်ရင် compiler က "data class must have at least one primary constructor parameter" ဒါမှမဟုတ် "primary constructor of data class must only have property ('val' / 'var') parameters" လို့ ပြောပါတယ်။
လူတွေကို အံ့ဩစေတဲ့ အသေးစိတ်က primary constructor property တွေပဲ ပါဝင်တာပါ။ Class body ထဲမှာ ကြေညာတဲ့ property ကို equals, hashCode, toString, copy နဲ့ componentN တို့က လျစ်လျူရှုပါတယ်။ body ထဲမှာ lastSeen ကြေညာထားတဲ့ profile နှစ်ခုက lastSeen မတူပေမယ့် တန်းတူပါပဲ၊ copy() ကလည်း အဲဒီ property ကို initial value နဲ့ ပြန်ထုတ်ပေးပါတယ်။ ဒါက cache လုပ်ထားတဲ့ ဒါမှမဟုတ် ရလာတဲ့ state အတွက် အသုံးဝင်ပေမယ့် body property က data အစစ်ဖြစ်နေရင်တော့ အန္တရာယ်ရှိပါတယ်။
copy() က property တစ်ခုစီအတွက် နာမည်ပေးပြီး default ပါတဲ့ parameter တွေ ရှိလို့ user.copy(name = "Bob") က field တစ်ခုကိုပဲ ပြောင်းပြီး ကျန်တာ ထိန်းထားပါတယ်။ ဒါက shallow copy ပါ — property တစ်ခုက mutable list ကို ကိုင်ထားရင် copy နဲ့ မူရင်းက list တစ်ခုတည်းကို မျှဝေပါတယ်။ ဒါကြောင့် val property တွေနဲ့ read-only collection type တွေကို ဦးစားပေးပါ၊ ဒါမှ မျှဝေတာ အန္တရာယ်မရှိတော့ပါဘူး။ val (name, age) = user destructuring က component1() နဲ့ component2() ကို အစဉ်အတိုင်း (နာမည်နဲ့ မဟုတ်ဘဲ) သုံးပါတယ်။ Property တွေကို အစဉ်ပြောင်းလိုက်ရင် type တူနေတဲ့အခါ destructure လုပ်ထားတဲ့ နေရာတိုင်းက အဓိပ္ပာယ် တိတ်တိတ်လေး ပြောင်းသွားပါတယ်။
Data class ကို HashMap key ဒါမှမဟုတ် HashSet member အဖြစ် သုံးတဲ့အခါ ဂရုစိုက်ပါ။ Hash code ကို property တန်ဖိုးတွေကနေ တွက်တာမို့ ထည့်ပြီးနောက် var property ကို ပြောင်းရင် hash ပြောင်းသွားပြီး object က set ထဲမှာ ပျောက်သွားပါတယ်။ Key ဖြစ်မယ့် အရာတိုင်းမှာ val property တွေကိုပဲ သုံးပါ။ ပြီးတော့ data class က interface တွေ implement လုပ်နိုင်ပြီး function တွေလည်း ပါနိုင်ပေမယ့် open, abstract, sealed ဒါမှမဟုတ် inner မဖြစ်နိုင်ပါဘူး၊ ဒါက value semantics ကို ရိုးရှင်းစေပါတယ်။
| ထုတ်ပေးတဲ့ member | ဘာကို သုံးလဲ | ဥပမာ |
|---|---|---|
| toString() | Primary property တွေ | User(name=Alice, age=28) |
| equals() / hashCode() | Primary property တွေ | User("A", 1) == User("A", 1) က true |
| copy(...) | Primary property နဲ့ default တွေ | user.copy(age = 29) |
| componentN() | Primary property တွေ၊ အစဉ်အလိုက် | val (name, age) = user |
- Primary constructor မှာ field တွေ စာရင်းပြု
- Compiler က ဒီကနေ member ငါးခု ရေး
- အကြောင်းအရာတူရင် object တူ
- copy() က ပြောင်းထားတဲ့ ညီအစ်ကို ဆောက်
Data class မှာ object ကို ခွဲခြားတာ field တွေရဲ့ တန်ဖိုးပါ၊ memory ထဲက တည်နေရာ မဟုတ်ပါဘူး။
အထွေထွေ ပုံစံ
data class Name(val first: Type, val second: Type = defaultValue) {
// properties declared here are NOT part of equals, toString or copy
}
val a = Name(x, y)
val b = a.copy(first = newValue) // changed twin, other fields kept
val same = a == Name(x, y) // compares contents
val (p, q) = a // component1(), component2()Primary constructor ထဲက property တွေပဲ generated member တွေမှာ ပါပါတယ်။ copy() က နာမည်ပေးထားတဲ့ field တစ်ခုကိုပဲ ပြောင်းပြီး ကျန်တာ ထိန်းပေးပါတယ်။ == က အကြောင်းအရာကို နှိုင်းယှဉ်ပြီး (p, q) = a က component1() နဲ့ component2() ကို အစဉ်လိုက် သုံးပါတယ်။
Example: copy ကူးခြင်း၊ နှိုင်းယှဉ်ခြင်းနဲ့ destructuring
User data class တစ်ခုက ဘာတွေ အလိုအလျောက် ရလဲ ကြည့်ရအောင်။ user2 ကို user1.copy(name = "Bob") နဲ့ ဆောက်တာကြောင့် age ကို ပြန်မပေးရဘဲ ကျန်ခဲ့ပါတယ်။ println က toString() ကို ခေါ်ပြီး အလွယ်တကူ ဖတ်လို့ရတဲ့ ပုံစံ ထုတ်ပေးပါတယ်။ == က အကြောင်းအရာကို နှိုင်းယှဉ်လို့ User("Alice", 28) အသစ်နဲ့တောင် true ရပြီး val (name, age) က user2 ကို ခွဲထုတ်ပါတယ်။
data class User(val name: String, val age: Int)
fun main() {
val user1 = User("Alice", 28)
val user2 = user1.copy(name = "Bob")
println(user1)
println(user2)
println(user1 == User("Alice", 28))
val (name, age) = user2
println("$name is $age")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Declare the data class
data class User(val name: String, val age: Int)data keyword ကြောင့် compiler က primary constructor ထဲက name နဲ့ age ကနေ toString, equals, hashCode, copy နဲ့ component1/component2 ကို ထုတ်ပေးပါတယ်။ Body မလိုလို့ ကွင်းကောက် မရေးပါဘူး။
- Copy with one change
val user2 = user1.copy(name = "Bob")copy က user1 ကို မပြောင်းပါဘူး၊ name ကို Bob နဲ့ အစားထိုးထားပြီး age ကို ကူးလာတဲ့ User အသစ်တစ်ခု ပြန်ပေးပါတယ်။ ဒါကြောင့် user1 က Alice ပဲ ရှိနေပါဦးမယ်။ Immutable data ကို "ပြောင်းတာ" ဆိုတာ ပြောင်းထားတဲ့ copy အသစ် ဆောက်တာပါ။
- Readable printing
println(user1)toString() ကို ခေါ်လို့ User(name=Alice, age=28) ထွက်ပါတယ်။ ပုံမှန် class ဆိုရင် User@ နဲ့ hex နံပါတ်ပဲ ပေါ်ပါလိမ့်မယ်။ Log နဲ့ test failure message တွေကို ဖတ်လို့ရတာ ဒီအတွက်ပါ။
- Compare contents
println(user1 == User("Alice", 28))== က equals() ကို ခေါ်ပြီး primary property တစ်ခုချင်း နှိုင်းယှဉ်ပါတယ်။ object အသစ်ဖြစ်ပေမယ့် တန်ဖိုးတူလို့ true ထွက်ပါတယ်။ Identity (တူတဲ့ object တစ်ခုတည်း လား) ကို စစ်ချင်ရင်တော့ === ကို သုံးပါ။
- Destructure
val (name, age) = user2ဒါက component1() နဲ့ component2() ကို ခေါ်ပြီး variable နှစ်ခု ဖန်တီးပါတယ်၊ အစဉ်အတိုင်းဖြစ်ပြီး နာမည်နဲ့ မဟုတ်ပါဘူး။ ဒါကြောင့် (age, name) လို့ ရေးမိရင် compile ဖြစ်ပြီး age ထဲကို နာမည် ရောက်သွားပါလိမ့်မယ်။ type မတူရင် နောက် line တစ်ခုခု ပျက်မှ သိပေမယ့် type တူတဲ့ field တွေမှာတော့ လုံးဝ မသိလိုက်ပါဘူး။
User(name=Alice, age=28)
User(name=Bob, age=28)
true
Bob is 28- a == b ကို တွက်
- equals() က primary property တွေကို တစ်ခုချင်း နှိုင်းယှဉ်
- အားလုံး တူလား
အားလုံးတူ
the result is true, even for two different objects
တစ်ခုတောင် မတူ
the result is false
Data အဖြစ် ရေတွက်တာ ဘာလဲ၊ copy က ဘယ်လောက်နက်လဲ
Profile မှာ name နဲ့ tags က primary constructor ထဲမှာ ရှိပြီး lastSeen က body ထဲမှာ ရှိပါတယ်။ ဒါကြောင့် a == b က lastSeen မတူရင်တောင် true ဖြစ်ပြီး print ထုတ်တဲ့အခါ lastSeen မပါပါဘူး။ a.copy() ကလည်း lastSeen ကို မကူးလို့ c.lastSeen က "never" ပြန်ဖြစ်ပါတယ်။ ပြီးတော့ c.tags.add("beta") က a.tags ကိုပါ ပြောင်းသွားပါတယ် — copy() က tags အတွက် list ကို ကူးတာမဟုတ်ဘဲ reference ကိုပဲ ကူးတာမို့ list တစ်ခုတည်းကို နှစ်ခုလုံးက မျှဝေလို့ပါ။
data class Profile(val name: String, val tags: MutableList<String>) {
var lastSeen: String = "never" // body property: not part of the data
}
fun main() {
val a = Profile("Aye", mutableListOf("admin"))
a.lastSeen = "monday"
val b = Profile("Aye", mutableListOf("admin"))
println(a == b) // true: lastSeen is not in the primary constructor
println(a) // Profile(name=Aye, tags=[admin])
println(b.lastSeen) // never
val c = a.copy()
println(c.lastSeen) // never: copy() passes only constructor properties
c.tags.add("beta") // shallow copy: the very same list object
println(a.tags) // [admin, beta]
}Collection တွေနဲ့ destructuring ထဲက data class
Content-based equality ကြောင့် data class တွေက collection တွေနဲ့ ကောင်းကောင်း အလုပ်လုပ်ပါတယ်။ items.toSet() ကို ခေါ်ရင် တန်းတူတဲ့ item တွေ တစ်ခုတည်း ဖြစ်သွားပြီး distinct() နဲ့ associateBy က နောက်ထပ် စုစည်းမှုတွေ ပေးပါတယ်။ items.map { it.copy(qty = it.qty + 10) } က မူရင်းစာရင်းကို မထိဘဲ ပြင်ထားတဲ့ စာရင်းအသစ်ကို ဆောက်ပါတယ်။ for ((sku, price, qty) in ...) က item တစ်ခုစီကို component1 ကနေ component3 အထိ ခွဲပြီး name ပေးထားတဲ့ variable သုံးခုပေးပါတယ်။ ဒါတွေက UI state နဲ့ test data တွေမှာ အရမ်းအသုံးများတဲ့ ပုံစံပါ။
data class Item(val sku: String, val price: Int, val qty: Int)
fun main() {
val items = listOf(
Item("A1", 10, 2),
Item("B2", 5, 1),
Item("A1", 10, 2),
Item("C3", 5, 4),
)
println(items.toSet().size) // 3: equal items collapse in a Set
val bySku = items.distinct().associateBy { it.sku }
println(bySku["B2"])
val restocked = items.map { it.copy(qty = it.qty + 10) }
println(restocked.first())
for ((sku, price, qty) in items.distinct()) println("$sku: ${price * qty}")
val groups = items.groupBy { it.price }
println(groups.keys)
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Data class တွေက လူတွေ ထင်တာထက် ပိုအသုံးများပါတယ်။ JSON library (kotlinx.serialization, Moshi, Jackson) တွေက API response ကို data class ရဲ့ constructor property တွေပေါ် တိုက်ရိုက် map လုပ်ပေးပါတယ်။ Database row တစ်ကြောင်းစီက data class တစ်ခုဖြစ်လို့ query ရလဒ်တွေကို list အဖြစ် ကိုင်ရတာ လွယ်ပါတယ်။ Test တွေမှာ assertEquals(expected, actual) က တန်ဖိုးတွေကို နှိုင်းယှဉ်ပြီး မကိုက်ရင် User(name=Alice, age=28) လို ဖတ်လို့ရတဲ့ မက်ဆေ့ချ်ကို ပြပေးပါတယ်။
UI state ကိုလည်း data class နဲ့ ထားလေ့ရှိပါတယ်။ Screen ရဲ့ state ကို val field တွေနဲ့ ကြေညာပြီး ပြောင်းလဲမှုတိုင်းမှာ state = state.copy(loading = false) လို့ copy အသစ်တစ်ခု ဆောက်ပြီး ပေးလိုက်ပါတယ်။ ဒါကြောင့် framework က အဟောင်းနဲ့ အသစ်ကို == နဲ့ နှိုင်းယှဉ်ပြီး ပြောင်းသွားမှသာ ပြန်ဆွဲပါတယ်။ ဒါပေမယ့် data class ဟာ identity ရှိတဲ့ entity တွေအတွက် မသင့်ပါဘူး — field တူတဲ့ order နှစ်ခုက တကယ်ကွဲပြားတဲ့ order တွေ ဖြစ်နိုင်လို့ id နဲ့ ခွဲရမယ့်အခါ ပုံမှန် class ကို ရွေးပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Body property တွေက equals နဲ့ copy ထဲ ပါမယ်လို့ ထင်တာ
data class Profile(val name: String) { var visits = 0 } မှာ visits က equals, toString နဲ့ copy မှာ မပါပါဘူး။ Profile("Aye") နှစ်ခုက visits မတူပေမယ့် တန်းတူပြီး copy() က visits ကို 0 ပြန်ဖြစ်စေပါတယ်။ Data အစစ်ဖြစ်ရင် constructor ထဲ ရွှေ့ပါ။
2. Shallow copy က mutable list ကို မျှဝေတာကို မသိတာ
val b = a.copy() ပြီး b.members.add(2) ဆိုရင် a.members ကိုပါ ပြောင်းသွားပါတယ်။ List<Int> လို read-only type နဲ့ ကြေညာပြီး ပြောင်းချင်ရင် members + 2 လို list အသစ် ဆောက်ပြီး copy(members = ...) လို့ ပေးပါ။
3. Set ထဲရောက်နေတဲ့ key ရဲ့ var property ကို ပြောင်းတာ
Hash code က property တန်ဖိုးတွေပေါ် မူတည်လို့ ထည့်ပြီးနောက် id ကို ပြောင်းရင် set က ပြန်ရှာမတွေ့တော့ပါဘူး၊ ပေမယ့် size မှာ ရှိနေဆဲပါ။ အောက်က snippet မှာ ကြည့်ပါ။ Set ဒါမှမဟုတ် map key အဖြစ် သုံးမယ့် data class တွေကို val property တွေနဲ့ပဲ ရေးပါ။
4. Property အစဉ်ပြောင်းပြီး destructuring ကို မစစ်တာ
Destructuring က နာမည်နဲ့ မဟုတ်ဘဲ အစဉ်နဲ့ အလုပ်လုပ်ပါတယ်။ Item(sku, price, qty) ကို Item(sku, qty, price) လို့ ပြောင်းလိုက်ရင် price နဲ့ qty ကို Int နှစ်ခုလုံး ဖြစ်လို့ compiler က မတားဘဲ တန်ဖိုးတွေ တိတ်တိတ်လေး လဲသွားပါတယ်။ Field နှစ်ခုထက်ပိုရင် componentN ထက် နာမည်နဲ့ ဖတ်တာ ပိုလုံခြုံပါတယ်။
data class Key(var id: Int)
fun main() {
val k = Key(1)
val set = hashSetOf(k)
println(k in set) // true
k.id = 2
println(k in set) // false: the hash changed, the object is lost
println(set.size) // 1: it is still in there
println(set.contains(Key(1))) // false: an equal-looking key is not found either
}Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Data class property တွေကို val နဲ့ ကြေညာပါ၊ ပြောင်းချင်ရင် copy() နဲ့ ပြောင်းထားတဲ့ object အသစ်ကို ဆောက်ပါ။
- MutableList လို mutable collection အစား List လို read-only type ကို သုံးပါ၊ ဒါမှ shallow copy က မျှဝေတာ အန္တရာယ်မရှိဘူး။
- Identity ရှိတဲ့ entity (id နဲ့ ခွဲရမယ့် order, account) တွေအတွက် data class မသုံးပါနဲ့၊ တန်ဖိုးလေးတွေ (Point, Money, Address) အတွက်ပဲ သုံးပါ။
Mini challenge
Mini challenge: Task list
Task data class တစ်ခုနဲ့ task list ကို ကိုင်တွယ်ပါ — ထပ်နေတာတွေ ဖယ်၊ priority မြင့်တာတွေကို ပြီးစီးအဖြစ် မှတ်ပြီး ရလဒ်ကို destructuring နဲ့ print ထုတ်ပါ။
- data class Task(title, priority = 1, done = false) ကို ကြေညာပါ
- complete(task) က done = true ပါတဲ့ copy ကို ပြန်ပေးပြီး မူရင်း task ကို မပြောင်းရ
- List ကို set အဖြစ် ပြောင်းပြီး ထပ်နေတဲ့ task တွေ ဖယ်ပါ၊ ပြီးရင် priority 2 နဲ့ အထက်ရှိတာတွေကို complete လုပ်ပါ
- Task တစ်ခုချင်းကို destructure လုပ်ပြီး print ထုတ်ပါ၊ ပြီးစီးသွားတဲ့ task ခေါင်းစဉ်တွေကို print ထုတ်ပါ၊ ပြီးတော့ tasks[0] က ပြီးစီးအဖြစ် copy ပြီး done ကို false ပြန်လုပ်ထားတဲ့ copy နဲ့ တူလား print ထုတ်ပါ
// TODO: data class Task(title, priority = 1, done = false)
fun complete(task: Task): Task {
// TODO: return a copy with done = true
}
fun main() {
// TODO: four tasks: "write report" (priority 2) twice, "email Aye", "book flight" (3)
// TODO: remove duplicates by going through a set, print how many remain
// TODO: complete every task with priority >= 2, print each as "title pN done=..."
// TODO: print the titles of the completed tasks (groupBy done)
// TODO: print tasks[0] == complete(tasks[0]).copy(done = false)
}ရလဒ်: Program က ဒီ line ခြောက်ကြောင်းကို ထုတ်ရပါမယ်: 3 write report p2 done=true email Aye p1 done=false book flight p3 done=true [write report, book flight] true
အရိပ်အမြွက် ကြည့်ရန်
tasks.toSet().toList() နဲ့ ထပ်နေတာ ဖယ်ပါ။ for ((title, priority, done) in updated) နဲ့ destructure လုပ်ပါ။ groupBy { it.done } က Map<Boolean, List<Task>> ပေးလို့ getValue(true).map { it.title } နဲ့ ခေါင်းစဉ်တွေ ယူပါ။
အဖြေ ကြည့်ရန်
data class Task(val title: String, val priority: Int = 1, val done: Boolean = false)
fun complete(task: Task): Task = task.copy(done = true)
fun main() {
val tasks = listOf(
Task("write report", 2),
Task("email Aye"),
Task("write report", 2),
Task("book flight", 3),
)
val unique = tasks.toSet().toList()
println(unique.size)
val updated = unique.map { if (it.priority >= 2) complete(it) else it }
for ((title, priority, done) in updated) println("$title p$priority done=$done")
val byDone = updated.groupBy { it.done }
println(byDone.getValue(true).map { it.title })
println(tasks[0] == complete(tasks[0]).copy(done = false))
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- data class က primary constructor property တွေကနေ equals, hashCode, toString, copy နဲ့ componentN ကို ထုတ်ပေးပြီး == က အကြောင်းအရာကို နှိုင်းယှဉ်ပါတယ်။
- Primary constructor မှာ parameter တစ်ခုအနည်းဆုံးရှိပြီး အားလုံး val/var ဖြစ်ရပါမယ်၊ body ထဲက property တွေ မပါပါဘူး။
- copy() က property တစ်ခုချင်း ပြောင်းထားတဲ့ object အသစ်ကို ဆောက်ပေမယ့် shallow ဖြစ်လို့ mutable list တွေကို မျှဝေပါတယ်။
- Set/map key အဖြစ်သုံးရင် val property တွေကိုပဲ သုံးပါ၊ destructuring က နာမည်နဲ့ မဟုတ်ဘဲ အစဉ်နဲ့ အလုပ်လုပ်ပါတယ်။
Kotlin language guide — Kotlin