ရိုးရိုးလေး ရှင်းပြမယ်
Generics & Extensions ကို ရိုးရိုးလေးနားလည်မယ်
Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
ဖြစ်လေ့ရှိတဲ့အမှား
Force unwrap သို့unchecked shared mutable state ဖြင့်compiler safety ကိုကျော်ခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Value / reference
- Protocol + type composition
- Reusable behavior
Generics & Extensions ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
func first<T>(in values: [T]) -> T? {
values.first
}
extension Array where Element: Numeric {
var total: Element { reduce(0, +) }
}
print([1, 2, 3].total)Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Value / reference
func first<T>(in values: [T]) -> T? {Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Protocol + type composition
extension Array where Element: Numeric {Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Reusable behavior
print([1, 2, 3].total)Generics & Extensions example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Value / reference
- Protocol + type composition
- Reusable behavior
Success
Generics & Extensions ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
func first<T>(in values: [T]) -> T? {
values.first
}
extension Array where Element: Numeric {
var total: Element { reduce(0, +) }
}
print([1, 2, 3].total)6Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Generics & Extensions example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Generics & Extensions example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
func first<T>(in values: [T]) -> T? {
values.first
}
extension Array where Element: Numeric {
var total: Element { reduce(0, +) }
}
print([1, 2, 3].total)ရလဒ်: Generics & Extensions ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Generics & Extensions example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Value / reference → Protocol + type composition → Reusable behavior
- Generics & Extensions ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
- Generics & Extensions example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
The Swift Programming Language — Swift.org