Thuta Learning
Dart
BasicProgrammingအခြေခံ

Operators

Dartသင်ခန်းစာ 835 မိနစ်

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

  • Arithmetic operator တွေကို သုံးပြီး / ~/ % ရဲ့ ရလဒ်ကို အနုတ်ကိန်းအပါအဝင် ကြိုခန့်မှန်းနိုင်ဖို့
  • Comparison နဲ့ logical operator တွေကို short-circuit နဲ့ precedence ကို နားလည်ပြီး ပေါင်းစပ်နိုင်ဖို့
  • ??, ??=, ?. နဲ့ ! ကို ဘယ်အချိန် သုံးသင့်လဲ၊ ဘာအန္တရာယ်ရှိလဲ ရှင်းပြနိုင်ဖို့
  • Cascade (..) နဲ့ type test (is, as) ကို သုံးနိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Operator ဆိုတာ value တွေကို ဘာလုပ်ရမလဲ Dart ကို ပြောတဲ့ သင်္ကေတလေးတွေပါ။ ပေါင်းမယ်၊ နှိုင်းယှဉ်မယ်၊ ဟုတ်/မဟုတ် အဖြေတွေကို ပေါင်းစပ်မယ်ဆိုတာတွေကို ဂဏန်းတွက်စက်ရဲ့ ခလုတ်တွေလို မြင်ကြည့်ပါ။ Dart မှာတော့ ခလုတ်တွေအပြင် "တန်ဖိုး မရှိနေနိုင်ဘူး" ဆိုတဲ့ အခြေအနေအတွက် အထူးခလုတ်တချို့ ပါပါတယ်။

Operator တိုင်းက operand တွေကို ယူပြီး သတ်မှတ်ထားတဲ့ type တစ်ခုရဲ့ ရလဒ်ကို ပြန်ပေးပါတယ်။ ဥပမာ / က double ကို ပြန်ပြီး comparison တွေက bool ကို ပြန်ပါတယ်။ ရလဒ် type ကို သိထားရင် code ကို မ run ခင်တောင် ဘာထွက်မလဲ ခန့်မှန်းလို့ရပါတယ်။

Problem → Why → Solution

ပြဿနာ: တူတဲ့ သင်္ကေတ၊ မတူတဲ့ အပြုအမူ

တခြား language က ဝင်လာတဲ့သူတွေ Dart operator တွေမှာ အံ့ဩစရာ နှစ်ခု ကြုံတတ်ပါတယ်။ ပထမတစ်ခုက အပတ်ထဲက နေ့ကို ပြန်ပတ်တဲ့ (today - 3) % 7 ပါ။ JavaScript, Java, C မှာ today က 1 ဆိုရင် -2 ထွက်ပြီး ရက်နံပါတ် အနုတ်ကို index လုပ်မိရင် crash ဖြစ်ပါတယ်။ Dart မှာတော့ အကြွင်းက အနုတ် ဘယ်တော့မှ မဖြစ်လို့ 5 ထွက်ပါတယ်။ ဒုတိယတစ်ခုက user က nickname မထည့်ထားတဲ့အခါ nickname.length ကို တိုက်ရိုက်သုံးတာပါ၊ Dart က ဒါကို compile မလုပ်ပါဘူး။

surprises.dart
void main() {
  int today = 1;                       // Monday
  int threeDaysAgo = (today - 3) % 7;  // -2 in many languages, but in Dart?
  print(threeDaysAgo);

  String? nickname;                    // the user never set one
  print(nickname.length);              // error: nickname may be null
  int half = 10 / 4;                   // error: / produces a double
}

Operator တွေက language အလိုက် မတူနိုင်လို့ပါ

% လို သင်္ကေတတွေကို language တစ်ခုနဲ့တစ်ခု မတူတဲ့ စည်းမျဉ်းနဲ့ သတ်မှတ်ထားလို့ အလေ့အထကို မှတ်ထားရုံနဲ့ မလုံလောက်ပါဘူး။ Dart ရဲ့ စည်းမျဉ်းတွေကို တိတိကျကျ သိရင် (-7 % 3 က 2၊ (-7).remainder(3) က -1) bug တွေ မဖြစ်ခင် ကာကွယ်နိုင်ပါတယ်။ null-aware operator တွေကလည်း တန်ဖိုး မရှိနိုင်တဲ့ နေရာတိုင်းမှာ ပြဿနာကို compile time မှာ တွေ့စေပြီး ဖြေရှင်းနည်း (?? သို့မဟုတ် ?.) ကို လက်ထဲ ထည့်ပေးပါတယ်။

Core concept: operator အုပ်စုတွေနဲ့ ရလဒ် type

Arithmetic operator တွေက + - * / ~/ % ပါ။ / က double ကို အမြဲ ပြန်ပြီး ~/ က ရလဒ်ကို သုညဘက်သို့ ဖြတ်လို့ -7 ~/ 2 က -3 ဖြစ်ပါတယ်။ % ကတော့ Euclidean modulo ဖြစ်လို့ ရလဒ်က အနုတ် မဖြစ်ပါဘူး၊ -7 % 3 က 2 ပါ။ (-7).remainder(3) ကတော့ ဘယ်ဘက် operand ရဲ့ လက္ခဏာကို လိုက်လို့ -1 ပါ။ int ကို သုညနဲ့ စားရင် (1 ~/ 0 ဒါမှမဟုတ် 1 % 0) IntegerDivisionByZeroException ပစ်ပါတယ်၊ 1 / 0 ကတော့ Infinity ပါ။ + က String ကိုလည်း ဆက်ပေးပြီး * က string ကို ပြန်ထပ်ပေးပါတယ် ('ab' * 3 က 'ababab')၊ ဒါပေမယ့် 'a' + 1 က compile error ပါ။

Comparison operator တွေ (== != > < >= <=) က bool ပြန်ပါတယ်။ == က number နဲ့ string အတွက် တန်ဖိုးကို နှိုင်းယှဉ်လို့ 1 == 1.0 က true ပါ၊ ဒါပေမယ့် list နှစ်ခုက element တူတာတောင် == နဲ့ ယှဉ်ရင် false ပါ၊ ဘာလို့လဲဆို list တွေက object အမှတ်အသား (identity) နဲ့ ယှဉ်လို့ပါ။ 1 < a < 10 လို chain လုပ်လို့ မရပါဘူး၊ (1 < a) && (a < 10) လို့ ရေးရပါမယ်။ Condition နေရာမှာ bool ပဲ လက်ခံပါတယ်၊ if (count) လို int ကို ရေးလို့ မရပါဘူး။

Logical operator တွေဖြစ်တဲ့ && || ! က short-circuit လုပ်ပါတယ်၊ && ရဲ့ ဘယ်ဘက်က false ဆိုရင် ညာဘက်ကို လုံးဝ မတွက်ပါဘူး၊ || ရဲ့ ဘယ်ဘက်က true ဆိုရင်လည်း အတူတူပါပဲ။ ဒါကြောင့် stock > 0 && 100 ~/ stock > 5 က stock က 0 ဖြစ်တဲ့အခါ သုညနဲ့စားတဲ့ crash ကို ကာကွယ်ပေးပါတယ်။ Precedence အရ ! က && ထက်၊ && က || ထက် အရင် အလုပ်လုပ်ပါတယ်၊ ဒါကြောင့် true || false && false က true ပါ။ Ternary cond ? a : b က အတိုချုပ် if/else ပါ။

Assignment မှာ = အပြင် += -= *= ~/= ပါပြီး ++ နဲ့ -- ကို prefix၊ postfix နှစ်မျိုးလုံး သုံးလို့ရပါတယ်။ i++ က မူလတန်ဖိုးကို ပြန်ပေးပြီးမှ တိုးပေးပြီး ++i ကတော့ အရင်တိုးမှ ပြန်ပေးပါတယ်။ Null-aware operator တွေကတော့ ?? (null ဖြစ်ရင် နောက်တန်ဖိုး သုံး)၊ ??= (null ဖြစ်နေမှသာ assign လုပ်)၊ ?. (null ဆိုရင် error မဟုတ်ဘဲ null ပြန်) နဲ့ ! (null မဟုတ်ဘူးလို့ ကိုယ်တိုင် အာမခံ) တို့ပါ။ ! ကိုသုံးပြီး တကယ် null ဖြစ်နေရင် run time မှာ "Null check operator used on a null value" နဲ့ crash ဖြစ်ပါတယ်။

နောက်ဆုံးအုပ်စုက object နဲ့ type နဲ့ ဆိုင်ပါတယ်။ is က type စစ်တာ၊ is! က မဟုတ်ဘူးလို့ စစ်တာ၊ as က cast လုပ်တာပါ၊ as က မှားရင် "type 'String' is not a subtype of type 'int' in type cast" လို error ပစ်ပါတယ်။ Cascade (..) ကတော့ object တစ်ခုတည်းပေါ်မှာ ခေါ်ချက်တွေ ဆက်တိုက်လုပ်ပြီး ရလဒ်အဖြစ် အဲဒီ object ကိုပဲ ပြန်ပေးပါတယ်။

ဂဏန်းတွက်စက်ရဲ့ ခလုတ်တွေ
  1. Operand တွေ ဝင်လာ
  2. Operand type အရ စည်းမျဉ်း ရွေး
  3. ရလဒ် type က သတ်မှတ်ပြီးသား
  4. Precedence က အစဉ်ကို ဆုံးဖြတ်

သံသယရှိရင် ကွင်း ( ) ခတ်ပါ၊ ပိုရှင်းပြီး precedence ကို မှတ်စရာ မလိုပါဘူး။

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

dart
result = left + right;           // arithmetic: + - * / ~/ %
flag = left >= right && ready;   // compare first, then combine
value = maybeNull ?? fallback;   // fallback when the left side is null
maybeNull ??= fallback;          // assign only while it is null
length = maybeNull?.length;      // null instead of an error
object..first()..second();       // cascade: many calls, one object

အပေါ်ဆုံး နှစ်ကြောင်းက ဂဏန်းနဲ့ ယုတ္တိ တွက်ချက်မှုတွေပါ။ အောက်က လေးကြောင်းက null-aware operator တွေနဲ့ cascade ပါ၊ တစ်ခုချင်းစီက အထူးအခြေအနေတစ်ခုကို တိုတိုနဲ့ ရှင်းရှင်း ကိုင်တွယ်ပေးပါတယ်။

Example: ဈေးနှုန်း၊ အကြွင်းနဲ့ coupon

ဒီ example က ဈေးနှုန်းကနေ လျှော့ဈေးကို နှုတ်ပြီး ဝယ်နိုင်မနိုင်ကို && နဲ့ စစ်ပါတယ်။ ပြီးတော့ % နဲ့ ~/ ကို အနုတ်ကိန်းနဲ့ စမ်းပါမယ်။ နောက်ဆုံးအပိုင်းမှာ coupon ရှိတာ၊ မရှိတာကို ?? နဲ့ ??= ကို သုံးပြီး ကိုင်တွယ်ပြပါမယ်။

operators.dart
void main() {
  int price = 12000;
  int discount = 2000;
  int finalPrice = price - discount;

  bool hasEnoughMoney = finalPrice <= 10000;
  bool isMember = true;

  print('Final price: $finalPrice');

  if (hasEnoughMoney && isMember) {
    print('You can buy this item with member benefit.');
  }

  print(7 % 2);     // remainder
  print(-7 % 3);    // % is never negative here
  print(-7 ~/ 2);   // truncates toward zero

  String? coupon;
  String label = coupon ?? 'no coupon';
  coupon ??= 'WELCOME';
  print(label);
  print(coupon?.length);
}

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

  1. Line 4int finalPrice = price - discount;

    int နှစ်ခုကို နုတ်တာမို့ ရလဒ်က int ပါ၊ 12000 - 2000 က 10000 ဖြစ်ပါတယ်။

  2. Line 6bool hasEnoughMoney = finalPrice <= 10000;

    <= က နှိုင်းယှဉ်ပြီး bool ကို ပြန်ပါတယ်။ 10000 <= 10000 မို့ true ပါ။

  3. Line 11if (hasEnoughMoney && isMember) {

    && က နှစ်ဖက်လုံး true ဖြစ်မှ true ပါ။ ဘယ်ဘက်က false ဆိုရင် ညာဘက်ကို မစစ်တော့ပါဘူး။

  4. Line 16print(-7 % 3);

    % က Euclidean modulo ဖြစ်လို့ အနုတ်ကိန်းကို စားတာတောင် ရလဒ် 2 ပါ။ C သို့မဟုတ် Java မှာဆိုရင် -1 ထွက်ပါလိမ့်မယ်။

  5. Line 17print(-7 ~/ 2);

    -3.5 ကို သုညဘက်ကို ဖြတ်လို့ -3 ပါ၊ -4 မဟုတ်ပါဘူး (floor မဟုတ်ဘဲ truncate ပါ)။

  6. Line 20String label = coupon ?? 'no coupon';

    coupon က null ဖြစ်နေလို့ ?? ရဲ့ ညာဘက်တန်ဖိုး 'no coupon' ကို label ထဲ ထည့်ပါတယ်။

  7. Line 21coupon ??= 'WELCOME';

    coupon က null ဖြစ်နေလို့ ဒီမှာ 'WELCOME' ကို assign လုပ်ပါတယ်။ နောက်ကြိမ် ထပ်ရေးရင် coupon က null မဟုတ်တော့လို့ ဘာမှ မပြောင်းပါဘူး။

You should see
Final price: 10000
You can buy this item with member benefit.
1
2
-3
no coupon
7

Precedence နဲ့ short-circuit

အစဉ် (မြင့်ကနေ နိမ့်)Operator တွေဥပမာ
1. Unary!x -x x++ ++x!ready
2. Multiplicative* / ~/ %2 + 3 * 4 က 14
3. Additive+ -a - b - c က ဘယ်မှညာ
4. Relational< > <= >= is asage >= 18
5. Equality== !=name == 'Aye'
6-7. Logical&& ပြီးမှ ||a || b && c
8-10. အနိမ့်ဆုံး?? ပြီးမှ ? : ပြီးမှ =x = a ?? b

အောက်က code မှာ precedence ကြောင့် ရလဒ်တွေ ဘာကြောင့် ဒီလိုထွက်လဲ၊ short-circuit ကြောင့် function တစ်ခု ဘာကြောင့် မခေါ်ခံရလဲ ကြည့်နိုင်ပါတယ်။ isReady() ကို false && ... နဲ့ true || ... ရဲ့ ညာဘက်မှာ ထားတဲ့အခါ "checked" ဆိုတဲ့ စာ တစ်ခါမှ မပေါ်ပါဘူး၊ နောက်ဆုံး true && isReady() မှသာ ပေါ်ပါတယ်။ ဒါကြောင့် ညာဘက်မှာ side effect ရှိတဲ့ function ကို ထားတဲ့အခါ သတိထားပါ။

precedence.dart
bool isReady() {
  print('checked');
  return true;
}

void main() {
  print(2 + 3 * 4);               // 14
  print((2 + 3) * 4);             // 20
  print(true || false && false);  // true: && binds tighter than ||

  int stock = 0;
  if (stock > 0 && 100 ~/ stock > 5) {
    print('plenty');
  } else {
    print('out of stock');        // the division was never attempted
  }

  print(false && isReady());      // isReady() is not called
  print(true || isReady());       // isReady() is not called
  print(true && isReady());       // prints checked, then true
}

Null-aware operator တွေနဲ့ cascade

?. က null ကို တွေ့တာနဲ့ ကျန်တဲ့ chain တစ်ခုလုံးကို ကျော်ပြီး null ပြန်ပါတယ်၊ ဒါကြောင့် nickname?.toUpperCase().length မှာ nickname က null ဆိုရင် toUpperCase ကိုလည်း length ကိုလည်း မခေါ်ပါဘူး။ ?? နဲ့ တွဲပြီး nickname?.length ?? 0 လို့ ရေးရင် null အတွက် ပုံသေ တန်ဖိုးတစ်ခု ပေးနိုင်ပါတယ်။ Cascade ကတော့ StringBuffer ဒါမှမဟုတ် list လို object တစ်ခုပေါ်မှာ ခေါ်ချက်တွေ ဆက်ရေးချင်တဲ့အခါ variable နာမည်ကို ထပ်ခါထပ်ခါ မရေးရအောင် ကူညီပါတယ်။ Cascade တစ်ခုလုံးရဲ့ တန်ဖိုးက ပထမ object ကိုယ်တိုင်ပါ၊ ကြားက method တွေရဲ့ ရလဒ်တွေ မဟုတ်ပါဘူး။

null_aware.dart
void main() {
  String? nickname;
  print(nickname ?? 'guest');
  print(nickname?.length);

  nickname ??= 'Aye';
  nickname ??= 'Ko';              // already set, so this line does nothing
  print(nickname);

  final buffer = StringBuffer()
    ..write('Total: ')
    ..write(1200)
    ..write(' MMK');
  print(buffer);

  final tags = <String>['a']..add('b')..addAll(['c', 'd']);
  print(tags);
}

! ကို အလွယ်တကူ မသုံးပါနဲ့

name! က compiler ကို "ဒါ null မဟုတ်ဘူး၊ ငါ အာမခံတယ်" လို့ ပြောတာပါ။ အာမခံချက် မှားရင် compiler က ကူညီမပေးတော့ဘဲ run time မှာ crash ဖြစ်ပါတယ်။ ?? နဲ့ fallback ပေးတာ၊ if (name != null) နဲ့ စစ်တာ ဒါမှမဟုတ် ?. သုံးတာတွေက ပိုလုံခြုံပါတယ်။

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

Real project တွေမှာ ဘယ်လို သုံးလဲ

Flutter မှာ Text(user?.name ?? 'Guest') လို ပုံစံက အဖြစ်အများဆုံးတစ်ခုပါ၊ ListView ထဲက အတန်းတစ်ခုချင်းစီကို အရောင်ခြားဖို့ index % 2 == 0 ကို သုံးပါတယ်။ Pagination မှာ စာမျက်နှာအရေအတွက်ကို (total + perPage - 1) ~/ perPage နဲ့ တွက်ပြီး စက္ကန့်တွေကို မိနစ်၊ စက္ကန့်ခွဲဖို့ ~/ 60 နဲ့ % 60 ကို သုံးပါတယ်။ Cache လုပ်ထားတဲ့ တန်ဖိုးကို တစ်ခါပဲ load လုပ်ဖို့ _cache ??= loadData() လို ရေးတာကိုလည်း အများကြီး တွေ့ရပါတယ်။

Backend နဲ့ command-line tool တွေမှာ guard condition တွေက short-circuit ကို အားကိုးပါတယ်၊ items.isNotEmpty && items.first > 0 လို့ ရေးရင် list အလွတ်မှာ crash မဖြစ်ပါဘူး။ JSON ကို ဖတ်တဲ့အခါ value is int လို type test နဲ့ ခွဲပြီးမှ အသုံးပြုကြပါတယ်။ Configuration object တွေကို ဆောက်ရာမှာ cascade က setting တွေကို အစဉ်လိုက် ထည့်ရတာ ရှင်းစေပါတယ်။ Code review မှာ reviewer တွေက ! နဲ့ as ကို အထူးစစ်ဆေးကြတာကလည်း ဒီ operator နှစ်ခုက run time မှာ crash ဖြစ်နိုင်လို့ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Comparison ကို chain လုပ်တာ

1 < a < 10 လို ရေးရင် "A comparison expression can't be an operand of another comparison expression." ဆိုတဲ့ error ထွက်ပါတယ်။ (1 < a) ကို ပထမ တွက်ပြီး ရလဒ် bool ကို 10 နဲ့ ထပ်ယှဉ်ရမှာမို့ အဓိပ္ပာယ် မရှိတာကြောင့်ပါ။ 1 < a && a < 10 လို့ ခွဲရေးပါ။

2. == အစား = ရေးမိတာ

if (count = 3) လို ရေးရင် "A value of type 'int' can't be assigned to a variable of type 'bool'." လို့ ပြပါတယ်၊ assignment က int ကို ပြန်ပြီး condition က bool လိုချင်လို့ပါ။ ကံကောင်းစွာ compiler က ဖမ်းပေးပေမယ့် error စာကို ဖတ်တဲ့အခါ = ကို တွေ့ရင် == လို့ ပြင်ပါ။

3. Null ကို handle မလုပ်ဘဲ ! နဲ့ ကျော်တာ

String? s; print(s!.length); ကို run ရင် "Null check operator used on a null value" နဲ့ ရပ်သွားပါတယ်။ ! က error ကို ဖျောက်ပေးတာမဟုတ်ဘဲ compile time ကနေ run time ကို ရွှေ့လိုက်တာပါ။ ?. နဲ့ ?? သို့မဟုတ် null စစ်ချက်နဲ့ အစားထိုးပါ။

4. သုညနဲ့ စားတာကို သတိမထားတာ

int တွေမှာ 1 ~/ 0 နဲ့ 1 % 0 က IntegerDivisionByZeroException ပစ်ပေမယ့် 1 / 0 က Infinity၊ 0.0 / 0.0 က NaN ထွက်ပြီး error မပေးပါဘူး။ ဒါကြောင့် ကိန်းပြည့်စားမယ့် divisor ကို စစ်ပါ၊ double ရလဒ်တွေကို print မလုပ်ခင် isNaN သို့မဟုတ် isInfinite နဲ့ စစ်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • && နဲ့ || ကို တစ်ပြိုင်နက် သုံးရင် ကွင်းခတ်ပါ၊ precedence ကို မှတ်ထားခိုင်းတာထက် ရေးထားတာကို ဖတ်ရလွယ်စေတာက ပိုကောင်းပါတယ်။
  • null ဖြစ်နိုင်တဲ့ တန်ဖိုးအတွက် x == null ? 'a' : x လို ternary အစား x ?? 'a' ကို ရွေးပါ။
  • ! နဲ့ as ကို တကယ် သေချာမှ သုံးပါ၊ အများအားဖြင့် is စစ်ချက်ကနေ type promotion ရတာ ပိုလုံခြုံပါတယ်။
  • ညာဘက်မှာ side effect ရှိတဲ့ function ကို && နဲ့ || ရဲ့ အတွင်း မထားပါနဲ့၊ short-circuit ကြောင့် မခေါ်ဘဲ ကျန်သွားနိုင်ပါတယ်။

Mini challenge

Mini challenge: အချိန်၊ နေ့နဲ့ နာမည်

operator သုံးမျိုးကို လက်တွေ့ အသုံးချပါ၊ စက္ကန့်ကို မိနစ်ခွဲပြတာ၊ အပတ်ထဲက နေ့ကို ပြန်ပတ်တာ၊ nickname မရှိတဲ့အခါ ကိုင်တွယ်တာ တို့ပါ။

  • String formatDuration(int totalSeconds) — ~/ နဲ့ % ကို သုံးပြီး '62 min 5 sec' ပုံစံ စာသား ပြန်ရမယ်
  • int weekdayAfter(int today, int offset) — 0 ကနေ 6 ကြား ရက်နံပါတ်ကို ပြန်ရမယ်၊ offset က အနုတ် ဖြစ်လည်း အလုပ်လုပ်ရမယ်
  • String greetingFor(String? nickname) — nickname ရှိရင် စာလုံးအကြီးနဲ့ 'Hello, AYE!'၊ မရှိရင် 'Hello, guest!' ပြန်ရမယ်
  • main မှာ formatDuration(3725), weekdayAfter(1, -3), weekdayAfter(5, 4), greetingFor(null), greetingFor('Aye') ကို print ထုတ်ပါ
helpers.dart
String formatDuration(int totalSeconds) {
  // TODO: split into whole minutes and leftover seconds
}

int weekdayAfter(int today, int offset) {
  // TODO: wrap into 0..6, even for negative offsets
}

String greetingFor(String? nickname) {
  // TODO: uppercase the nickname when present, otherwise use 'guest'
}

void main() {
  // TODO: print the five requested calls
}

ရလဒ်: 62 min 5 sec 5 2 Hello, guest! Hello, AYE!

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

3725 စက္ကန့်ကို 60 နဲ့ ~/ လုပ်ရင် မိနစ်၊ % လုပ်ရင် ကျန်စက္ကန့် ရပါတယ်။ အနုတ် offset အတွက် Dart ရဲ့ % က အနုတ်မဖြစ်တာကို အသုံးချပါ။ nickname အတွက် ?. နဲ့ ?? ကို တွဲပါ။

အဖြေ ကြည့်ရန်
helpers.dart
String formatDuration(int totalSeconds) {
  final minutes = totalSeconds ~/ 60;
  final seconds = totalSeconds % 60;
  return '$minutes min $seconds sec';
}

int weekdayAfter(int today, int offset) => (today + offset) % 7;

String greetingFor(String? nickname) {
  return 'Hello, ${nickname?.toUpperCase() ?? 'guest'}!';
}

void main() {
  print(formatDuration(3725));
  print(weekdayAfter(1, -3));
  print(weekdayAfter(5, 4));
  print(greetingFor(null));
  print(greetingFor('Aye'));
}

Quick Quiz 1 / 3

print(-7 % 3); က ဘာကို print ထုတ်မလဲ။

Quick Quiz 2 / 3

String? name; name ??= 'Aye'; name ??= 'Ko'; print(name); က ဘာထွက်မလဲ။

Quick Quiz 3 / 3

print(7 ~/ 2 + 7 % 2); က ဘာကို print ထုတ်မလဲ။

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

  • / က double ကို၊ ~/ က သုညဘက်သို့ ဖြတ်ထားတဲ့ int ကို ပြန်ပြီး % က Dart မှာ အနုတ်မဖြစ်ပါဘူး၊ (-7).remainder(3) ကသာ လက္ခဏာကို လိုက်ပါတယ်။
  • && နဲ့ || က short-circuit လုပ်ပြီး ! က && ထက်၊ && က || ထက် အရင်လုပ်ပါတယ်။ သံသယရှိရင် ကွင်းခတ်ပါ။
  • ?? က fallback ပေးတယ်၊ ??= က null ဖြစ်မှ assign လုပ်တယ်၊ ?. က null ဆိုရင် chain ကို ရပ်တယ်၊ ! ကတော့ run time မှာ crash ဖြစ်နိုင်ပါတယ်။
  • Cascade (..) က object တစ်ခုတည်းပေါ် ခေါ်ချက်တွေ ဆက်လုပ်ပြီး အဲဒီ object ကိုပဲ ရလဒ်အဖြစ် ပြန်ပေးပါတယ်။

Dart language overview — Dart

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

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

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

Operators | Thuta Learning