ရိုးရိုးလေး ရှင်းပြမယ်
Dart ဆိုတာ ရိုးရိုးလေး ဘာလဲ
Dart ဆိုတာ Google က ထုတ်လုပ်ပြီး open source အဖြစ် ထုတ်ပေးထားတဲ့ general-purpose ဘာသာစကားတစ်ခုပါ။ ဖုန်း၊ browser နဲ့ desktop မှာ user interface ပါတဲ့ app တွေကို ရေးရတာမြန်ပြီး run တာလည်း မြန်အောင် ဒီဇိုင်းလုပ်ထားတာပါ။ အခုအချိန်မှာ Dart ကို အများဆုံး တွေ့ရတာက Flutter ပါ၊ အဲဒီ toolkit ရဲ့ app တွေကို Dart နဲ့ ရေးကြတာပါ။ ဒါပေမယ့် Dart ကို server၊ script နဲ့ command-line tool တွေ ရေးဖို့လည်း သုံးပါတယ်။
Java၊ Kotlin၊ C# ဒါမှမဟုတ် TypeScript ကို သိရင် Dart က ရင်းနှီးပုံ ပေါ်ပါလိမ့်မယ်: curly brace၊ class နဲ့ type တွေပါ။ Scripting language သာ သိရင်တော့ အသစ်ဖြစ်တဲ့ idea က Dart က value တိုင်းရဲ့ အမျိုးအစားကို စောစောကတည်းက သိချင်တာပါ၊ ဒါမှ စောစောကတည်းက သတိပေးနိုင်မှာမို့ပါ။
Problem → Why → Solution
ပြဿနာ: ပထမဆုံး program ကိုတောင် Dart က ငြင်းတယ်
သင့်ရဲ့ ပထမဆုံး Dart program က နှစ်ကြောင်းပဲ ရှည်ပြီး Dart က run ဖို့ ငြင်းပါတယ်။ Message က hello.dart:3:23: Error: Expected ';' after this. ပါ။ Scripting language တော်တော်များများမှာ line အဆုံး မပါတာကို ခွင့်လွှတ်ပေမယ့် Dart မှာတော့ statement တိုင်းကို semicolon နဲ့ ပိတ်ရပါတယ်၊ ပြီးတော့ error ကို semicolon ကျန်တဲ့ line ရဲ့ အဆုံးမှာ ပြတာဖြစ်ပြီး နောက် line မှာ မပြပါဘူး။
အဲဒီ error သေးသေးလေးရဲ့ နောက်မှာ ပိုကြီးတဲ့ စိုးရိမ်စရာ ရှိပါတယ်။ ဖုန်း app တစ်ခုကို ကိရိယာထောင်ချီမှာ install လုပ်ထားလို့ user က တွေ့တဲ့ crash က ဈေးကြီးပါတယ်၊ compiler က တွေ့တဲ့ အမှားကတော့ စက္ကန့်ပိုင်းပဲ ကုန်ပါတယ်။ Dart ရဲ့ ဒီဇိုင်း အဖြေက program မစခင် အမှားတွေကို အများဆုံး ဖမ်းဖို့ပါ: syntax မှား၊ type မှားတဲ့ value၊ တကယ့် value လိုတဲ့နေရာမှာ null ရှိနေတာတွေပါ။
// hello.dart
void main() {
print('Hello, Dart!')
}စောစော စစ်ရတာ ဘာကြောင့် တန်လဲ
Compile time မှာ စစ်တဲ့ check တိုင်းက user ဆီ ဘယ်တော့မှ မရောက်နိုင်တော့တဲ့ bug တစ်ခုပါ။ Dart က အဲဒါအတွက် တင်းကျပ်မှုနဲ့ ပေးရပါတယ်: semicolon လိုတယ်၊ type တွေကို သိထားတယ်၊ type က ခွင့်မပြုရင် variable မှာ null မထည့်နိုင်ဘူး။ ပထမနေ့ကတည်းက ဒီတင်းကျပ်မှုကို ရင်းနှီးထားရင် နောက် lesson တိုင်း ပိုလွယ်သွားပါလိမ့်မယ်။
အဓိက သဘောတရား: type စစ်ထားပြီး နည်းအမျိုးမျိုးနဲ့ run နိုင်တဲ့ ဘာသာစကား
Dart code ကို နည်းသုံးမျိုးနဲ့ run နိုင်ပြီး source file တစ်ခုတည်းကို အားလုံးအတွက် သုံးလို့ရပါတယ်။
| နည်း | ဘာဖြစ်လဲ | သုံးတတ်တဲ့နေရာ |
|---|---|---|
| Dart VM နဲ့ JIT | Program run နေတုန်း VM က just-in-time compile လုပ်တယ် | Develop လုပ်တာ၊ dart run၊ Flutter debug build |
| AOT native code | Target CPU အတွက် machine code အဖြစ် ကြိုတင် compile လုပ်တယ် | Release mobile နဲ့ desktop app၊ command-line executable |
| JavaScript သို့ WebAssembly | Dart code ကို JavaScript ဒါမှမဟုတ် Wasm အဖြစ် ဘာသာပြန်တယ် | Browser ထဲမှာ run တဲ့ web app |
Target ဘာပဲဖြစ်ဖြစ် ဘာသာစကားစည်းမျဉ်းတွေက အတူတူပါပဲ။ Dart က statically typed ဖြစ်ပါတယ်: int၊ double၊ String နဲ့ bool တို့က type တွေဖြစ်ပြီး compiler က variable တိုင်းနဲ့ expression တိုင်းရဲ့ type ကို မှတ်ထားပါတယ်။ Type တွေကို နေရာတိုင်းမှာ ရေးခိုင်းတာ မဟုတ်ပါဘူး၊ var လို့ရေးရင် value ကနေ Dart က type ကို infer လုပ်ပေးပါတယ်၊ ဒါပေမယ့် တစ်ခါ ဆုံးဖြတ်ပြီးရင် type က မပြောင်းပါဘူး။ Dart ရဲ့ type system က sound ဖြစ်ပါတယ်၊ ဆိုလိုတာက int type variable မှာ String ဘယ်တော့မှ မရောက်ဖို့ compiler နဲ့ runtime တို့ အတူ အာမခံပေးတာပါ။
Dart 3 မှာ sound null safety လည်း ပါပါတယ်။ String လို type က null ကို ဘယ်တော့မှ မကိုင်နိုင်ပါဘူး၊ တန်ဖိုးမရှိတာကို ခွင့်ပြုချင်ရင် String? လို မေးခွန်းအမှတ်အသား ထည့်ရပါတယ်။ တန်ဖိုး မပေးရသေးတဲ့ nullable variable မှာ null ရှိပြီး method တွေ မခေါ်ခင် အဲဒီ အခြေအနေကို ကိုင်တွယ်ဖို့ Dart က တောင်းဆိုပါတယ်။ ဒါက တခြားဘာသာစကားတွေက run time မှသာ ပြတတ်တဲ့ crash မျိုးစုကြီးကို ဖယ်ရှားပေးပါတယ်။
နောက်ဆုံးအနေနဲ့ Dart မှာ အားလုံးနီးပါးက object ပါ။ ကိန်းပြည့် 5 ကိုတောင် isEven လို method ပါတဲ့ int object တစ်ခုပါ၊ function တွေလည်း ပေးပို့လို့ရတဲ့ object တွေပါ။ Program က top-level function main() ကနေ စပြီး statement တွေကို semicolon နဲ့ ပိတ်ကာ print() က value ကို စာသားအဖြစ် ရေးပေးပါတယ်။
- Type ပါတဲ့ Dart source code
- Analyzer နဲ့ compiler က စစ်မယ်
- VM၊ native code ဒါမှမဟုတ် JS က run မယ်
ဘာသာစကား စစ်ဆေးမှုတွေက target တိုင်းမှာ တူပါတယ်၊ platform library တွေပဲ ကွာပါတယ် (ဥပမာ dart:io က browser မှာ မရှိပါဘူး)။
အထွေထွေ ပုံစံ: variable၊ function နဲ့ main
Type name = value; // explicit type
var name = value; // type inferred from the value
Type? name; // nullable, starts as null
ReturnType name(Type param) => expression;
void main() {
print(expression);
}ပထမ line သုံးကြောင်းက အသုံးအများဆုံး variable ကြေညာနည်း သုံးမျိုးပါ: type ရေးထားတာ၊ type ကို infer လုပ်စေတာ၊ nullable type သုံးတာ။ Body က expression တစ်ခုတည်းဖြစ်တဲ့ function က braces နဲ့ return အစား => ကို သုံးလို့ရပါတယ်။ Program တိုင်းမှာ main() ကတော့ လိုနေဆဲပါ။
Dart ကို screen တစ်ခုထဲမှာ ကြည့်ခြင်း
ဒီ program က screen တစ်ခုထဲ ဝင်ပေမယ့် Dart ကို Dart ဖြစ်စေတဲ့ အရာလေးခုကို ပြပါတယ်: typed function၊ infer လုပ်ထားတဲ့ variable၊ nullable variable နဲ့ string interpolation ပါ။ Run မကြည့်ခင် output လေးကြောင်းလုံးကို၊ အထူးသဖြင့် နောက်ဆုံးနှစ်ကြောင်းကို ကြိုမှန်းကြည့်ပါ။
// A tiny program that shows what makes Dart, Dart.
int square(int n) => n * n;
void main() {
String language = 'Dart'; // explicit type
var chapters = 5; // type inferred: int
int? nickname; // nullable: allowed to hold null
print('Hello, $language!');
print('$chapters chapters, square = ${square(chapters)}');
print('nickname: $nickname');
print(chapters.runtimeType);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A typed function
int square(int n) => n * n;=> က body ထဲမှာ return တစ်ခုတည်း ရှိတဲ့ function အတွက် အတိုကောက်ပါ။ Parameter ရော ရလဒ်ပါ int ဖြစ်လို့ square('5') လို ခေါ်ရင် run မတိုင်ခင် ငြင်းပါမယ်။
- An explicit type
String language = 'Dart';Type ကို ကိုယ်တိုင်ရေးထားတာပါ။ language ထဲမှာ String ပဲ ထည့်လို့ရပါမယ်၊ language = 3 လို့ ရေးရင် compile error ပါ။
- An inferred type
var chapters = 5;Dart က 5 ကနေ chapters ကို int လို့ infer လုပ်ပါတယ်။ var က 'ဘယ်အမျိုးအစားမဆို' လို့ ဆိုလိုတာ မဟုတ်ပါဘူး၊ နောက်မှ chapters = 'five' လို့ ရေးရင် error ပါ။
- A nullable variable
int? nickname;int နောက်က ? က null ကို ခွင့်ပြုတယ်လို့ ပြောတာပါ။ တန်ဖိုး မပေးထားလို့ nickname မှာ null ရှိပါတယ်။ ? မပါရင် int nickname; က ဒီ program မှာ compile error ဖြစ်ပါလိမ့်မယ်။
- Interpolation
print('$chapters chapters, square = ${square(chapters)}');$chapters က variable တစ်ခုကို၊ ${ } က function call လို expression တစ်ခုလုံးကို စာသားထဲ ထည့်ပေးပါတယ်။ ဒီ line က '5 chapters, square = 25' ကို print ထုတ်ပါတယ်။
- Types at run time
print(chapters.runtimeType);runtimeType က value ရဲ့ တကယ့် type ကို ပြပါတယ်၊ ဒီမှာ int ပါ။ Dart မှာ type တွေက run time မှာလည်း ရှိနေပါတယ်။
Hello, Dart!
5 chapters, square = 25
nickname: null
intသိထားပြီးသား ဘာသာစကားတွေနဲ့ နှိုင်းယှဉ်ခြင်း
| ခေါင်းစဉ် | Dart မှာ |
|---|---|
| Type | Inference ပါတဲ့ static type၊ TypeScript လို မဖျက်ဘဲ run time မှာလည်း type တွေ ကျန်ပါတယ် |
| Null | Type တွေက default အရ null မရဘူး၊ ? ထည့်မှ ရတယ် (Kotlin နဲ့ ဆင်တူ) |
| စားခြင်း | / က double အမြဲပြန်တယ်၊ ~/ က int ရလဒ်ပေးတယ် |
| တစ်ပြိုင်နက်လုပ်ခြင်း | Isolate တစ်ခုချင်းမှာ event loop တစ်ခု၊ isolate တွေက memory မမျှဝေဘူး |
| Semicolon | Statement တိုင်းရဲ့ အဆုံးမှာ လိုတယ် |
Dart 3 က record၊ pattern နဲ့ class modifier (sealed, base, interface, final) တွေကို ထပ်ထည့်ပေးခဲ့ပါတယ်။ နောက်ပိုင်း အခန်းတွေမှာ သင်ပေးပါမယ်။
int နဲ့ double: နံပါတ်နှစ်မျိုးကို မှတ်ထားပါ
Dart က ကိန်းပြည့်နဲ့ ဒသမကိန်းကို int နဲ့ double ဆိုပြီး အမျိုးအစားနှစ်မျိုး ခွဲထားပါတယ်။ / operator က ဘယ်လိုပဲ ဖြစ်ဖြစ် double ပြန်ပေးတာကို စပြီး သတိထားရတဲ့ အချက်ပါ။ အောက်က snippet ကို run ကြည့်ရင် 3.5၊ 3၊ 5.0၊ 5 ဆိုပြီး ထွက်လာပါမယ်။ 10 / 2 က ရလဒ် အပြည့်ဖြစ်ပေမယ့် 5 မဟုတ်ဘဲ 5.0 ဖြစ်ပါတယ်။
void main() {
print(7 / 2); // / always returns a double
print(7 ~/ 2); // ~/ returns an int
print(10 / 2); // still a double
int whole = 10 ~/ 2;
print(whole);
}လက်တွေ့ project တွေမှာ
လက်တွေ့မှာ Dart ကို ဘယ်မှာ တွေ့လဲ
လက်တွေ့မှာ Dart ကို နေရာသုံးခုမှာ တွေ့ပါတယ်။ အကြီးဆုံးက Flutter app တွေပါ: codebase တစ်ခုတည်းက Android၊ iOS၊ web နဲ့ desktop ကို target လုပ်ပြီး develop လုပ်တုန်း သုံးတဲ့ hot reload က VM ရဲ့ JIT mode ကနေ လာတာပါ။ ဒုတိယက command-line tool တွေပါ: Dart program တစ်ခုကို dart compile exe နဲ့ သီးခြားရပ်တည်နိုင်တဲ့ executable အဖြစ် compile လုပ်ပြီး run မယ့် စက်မှာ Dart install မလိုဘဲ မြန်မြန် စတင်စေနိုင်ပါတယ်။ တတိယက server နဲ့ build code တွေပါ၊ backend၊ tooling နဲ့ app တို့က type နဲ့ library တွေ မျှဝေနိုင်အောင် team တွေက Dart ကို ရွေးကြပါတယ်။
နေရာသုံးခုလုံးက pub.dev package site နဲ့ dart command ကို မျှဝေသုံးကြတာမို့ တစ်နေရာမှာ သင်တာက တခြားနေရာတွေကို ကူးသွားပါတယ်။ ရက်စွဲ format လုပ်တဲ့ package၊ JSON parser ဒါမှမဟုတ် test runner က Flutter widget ထဲမှာဖြစ်ဖြစ်၊ command-line script ထဲမှာဖြစ်ဖြစ် အတူတူ အလုပ်လုပ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားများ
၁။ Semicolon ကျန်တာကို ခွင့်လွှတ်မယ်လို့ ထင်တာ
Error message က Expected ';' after this. လို့ ပြောပြီး semicolon ကျန်တဲ့ line ရဲ့ အဆုံးကို ညွှန်ပါတယ်။ Message မှာ ပေးထားတဲ့ line နံပါတ်ကို ကြည့်ပြီး အဲဒီ line ရဲ့ အဆုံးမှာ ; ထည့်ပါ၊ နောက် line ကို မရှာပါနဲ့။
၂။ JavaScript ပုံစံ စားခြင်းကို မျှော်လင့်တာ
(10 + 20) / 2 က 15 မဟုတ်ဘဲ 15.0 ကို print ထုတ်ပါတယ်။ int total = 10 / 2; ကိုလည်း Error: A value of type 'double' can't be assigned to a variable of type 'int'. နဲ့ ငြင်းပါတယ်၊ ရလဒ်က အပြည့်ဖြစ်နေတာတောင် / က double ပြန်လို့ပါ။ int လိုချင်ရင် ~/ ကို သုံးပါ။
၃။ Dart ဆိုတာ Flutter လို့ ထင်တာ
package:flutter ကို import လုပ်တဲ့ file ကို dart run နဲ့ run ရင် အများအားဖြင့် မအောင်မြင်ပါဘူး၊ ဘာကြောင့်လဲဆိုတော့ Flutter ပဲ ပေးတဲ့ library (dart:ui လို) တွေကို plain Dart မှာ မပါလို့ပါ။ ဒီ course ရဲ့ လေ့ကျင့်ခန်းတွေကို plain Dart နဲ့ပဲ ထားပါ။
Mini challenge
Mini challenge: score အစီရင်ခံစာ
Function နှစ်ခု ရေးပါ: အမှတ်နှစ်ခုရဲ့ ပျမ်းမျှကို double အဖြစ် ပြန်ပေးတဲ့ average နဲ့ အမှတ် မရသေးသူအတွက် null ကို လက်ခံတဲ့ label ပါ။ ပြီးရင် main() ကနေ ခေါ်ပါ။
- average(int a, int b) က double ပြန်ရမယ် (average(10, 20) က 15.0 ကို print ထုတ်ရမယ်)
- label က int? score ကို လက်ခံပြီး null ဆိုရင် 'Name: no score yet'၊ မဟုတ်ရင် 'Name: 87' လို ပြန်ရမယ်
- main() မှာ average(3, 4)၊ average(10, 20)၊ label('Aye', 87) နဲ့ label('Ko', null) ကို print ထုတ်ပါ
- မ run ခင် output ကို ကြိုမှန်းပါ၊ 15.0 ကို သတိထားပါ
// TODO: a and b are int, the return type is double
average(a, b) {
// TODO: add the two scores and divide with /
}
// TODO: score may be missing, so its type needs a ? after int
String label(String name, score) {
// TODO: if score is null, return '<name>: no score yet'
// TODO: otherwise return '<name>: <score>'
}
void main() {
// TODO: print the four results listed in the requirements
}ရလဒ်: Program က အောက်ပါ line လေးကြောင်းကို print ထုတ်ပါမယ်: 3.5 15.0 Aye: 87 Ko: no score yet
အရိပ်အမြွက် ကြည့်ရန်
(a + b) / 2 က double ဖြစ်လို့ return type ကို double လို့ ရေးပါ။ label ထဲမှာ if (score == null) { return ...; } ကို အရင်စစ်ပါ၊ ပြီးမှ score ကို string ထဲ ထည့်ပါ။
အဖြေ ကြည့်ရန်
double average(int a, int b) => (a + b) / 2;
String label(String name, int? score) {
if (score == null) {
return '$name: no score yet';
}
return '$name: $score';
}
void main() {
print(average(3, 4));
print(average(10, 20));
print(label('Aye', 87));
print(label('Ko', null));
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Dart က Google ထုတ်တဲ့ typed, null-safe, object-oriented ဘာသာစကားဖြစ်ပြီး VM (JIT)၊ native code (AOT)၊ JavaScript သို့ WebAssembly အဖြစ် run နိုင်ပါတယ်
- var က type တစ်ခုကို infer လုပ်ပြီး ပြောင်းလို့မရပါဘူး၊ type? က null ကို ခွင့်ပြုပါတယ်
- / က double အမြဲပြန်ပြီး ~/ က int ရလဒ်ပေးပါတယ်
- Program တိုင်းက main() ကနေ စပြီး statement တိုင်းကို semicolon နဲ့ ပိတ်ရပါတယ်
Dart 3.13 changelog — Dart