Pro Yearly is on sale from $80 to $50! »

Carefully use and Enjoy Static Extension Methods

Cfbe23392787cb3ad4689c5f72463fcc?s=47 makicamel
November 07, 2019

Carefully use and Enjoy Static Extension Methods

Cfbe23392787cb3ad4689c5f72463fcc?s=128

makicamel

November 07, 2019
Tweet

Transcript

  1. 4UBUJD&YUFOTJPO.FUIPET
 ࢖͏લʹ஌͓͖͍ͬͯͨ஫ҙॻ͖ 2019.11.7. Flutter Meetup Tokyo #12 @makicamel

  2. ࣗݾ঺հ •@makicamel / ઒ݪສق •Flutterྺ൒೥͘Β͍ •Ruby ͱϏʔϧɹɹͱ͓ञ͕޷͖

  3. ࣗݾ঺հ

  4. •ڥք஋ͱͯ͠ࠓ೔ਂ໷0࣌ͷDateTimeΠϯελϯε͕΄͍͠ DateCalc.now().beginningOfDay(); •aͱb͕ಉ͡೔͔Ͳ͏͔൑ఆ͍ͨ͠ a.isSameDay(b); •c͕Կϲ݄લͳͷ͔Λऔಘ͍ͨ͠ c.differenceValue(type: DateType.month); •1೥ޙͷࠓ೔ͷ೔෇Λग़͍ͨ͠ DateCalc.now().addYear(1); DateCalc

  5. Dart 2.6 is available IUUQTEBSUEFW

  6. IUUQTUXJUUFSDPNEBSU@MBOHTUBUVT

  7. Static Extension Methods %BSU4UBUJD&YUFOTJPO.FUIPET%FTJHOEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFCMPCFEDDCEBD⒎DD BDDFQUFEGVUVSFSFMFBTFTTUBUJDFYUFOTJPONFUIPETGFBUVSFTQFDJpDBUJPONE

  8. Static Extension Methods ? •طଘͷClassΛมߋɾ֦ு(Extend)͢Δߏจ ɹɹɹ֦ுؔ਺ ɹɹɹΤΫεςϯγϣϯ/Extension ɹɹɹΦʔϓϯΫϥε

  9. DateFormat('yyyy/MM/dd'). format(DateTime.now().add(Duration(days: 1))); ͜Ε·Ͱ •௕͍ •ͱ͘ʹ3͕ಡΈͮΒ͍ ᶃ ᶄ ᶅ ɹ໌೔ͷ೔෇ͷจࣈྻΛऔಘ͍ͨ͠

  10. doMyOtherStuff(doMyStuff(obj.doStuff()).doOtherStuff()); ᶃ ͜Ε·Ͱ ᶄ ᶅ ᶆ ᶅ

  11. (DateTime.now() + 1.days).toString('yyyy/MM/dd'); ͜Ε͔Β •γϯϓϧ DateTime.tomorrow().toString('yyyy/MM/dd'); ɹ໌೔ͷ೔෇ͷจࣈྻΛऔಘ͍ͨ͠

  12. obj.doStuff().doMyStuff().doOtherStuff().doMyOtherStuff(); ͜Ε͔Β

  13. 4UBUJD&YUFOTJPO5ZQFTEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT Static Extension Methods •Readability
 (Մಡੑ) •Discoverability(IDE Support)
 (ൃݟ༰қੑ(IDEͷαδΣετΛड͚΍͍͢))

    • Static Extension Types • Pipeline-operator ɹ΋ݕ౼த 1JQFMJOFPQFSBUPSGPSJOMJOFJOWPDBUJPOPGTUBUJDGVODUJPOTEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT
  14. How to try it IUUQTUXJUUFSDPN@NPOPTUBUVT stableҎ֎Ͱ2.6.0Ҏ্ͷਖ਼ࣜ൛͕ೖΓ·ͨ͠

  15. 1. flutter channel ɹࣗ෼ͷνϟϯωϧΛ֬ೝ 
 3 steps to try it

    ❯ flutter channel Flutter channels: beta dev master * stable ͠͹Β͘upgrade͍ͯ͠ͳ͍ਓ͸ flutter upgrade ΋ flutter upgrade
  16. 2. flutter channel master ɹνϟϯωϧ͕stableͳΒ੾Γସ͑ 
 3 steps to try

    it ❯ flutter channel master Switching to flutter channel 'master'... git: From https://github.com/flutter/flutter git: 55df81e8e..28b5cc382 git: master -> origin/master git: Switched to branch 'master'
  17. 3. flutter --version ɹμ΢ϯϩʔυͱϏϧυΛ૸ΒͤΔ 
 3 steps to try it

    ❯ flutter --version Flutter 1.10.15-pre.419 • channel master • https://github.com/flutter/flutter.git Framework • revision 02aac5072c (11 hours ago) • 2019-11-05 14:38:09 -0800 Engine • revision 0928c1af2f Tools • Dart 2.7.0 ४උ׬ྃ
  18. Static Extension Methodsͷߏจ extension ExtensionSupporterNum on Num { Duration get

    days => milliseconds * Duration.millisecondsPerDay; } Ϋϥε໊ ֦ுΫϥε໊͸࣮ࡍ࢖Θͳ͍ͷͰ িಥ͠ʹ໊͍͘લʹ͢Δ
  19. extension ExtensionSupporterNum on Num { Duration get days => milliseconds

    * Duration.millisecondsPerDay; } 1.days
  20. 1.days 1.days // => 24:00:00.000000

  21. ϋϚΓͲ͜Ζ •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍ •NNBD(Non-Null by Default; null҆શ)
 Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍ •ϝιου໊ͷিಥʹ஫ҙ

  22. ϋϚΓͲ͜Ζ •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍ •NNBD(Non-Null by Default; null҆શ)
 Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍ •ϝιου໊ͷিಥʹ஫ҙ

  23. DateTime() + 1.month •`+` ͷҾ਺ʹΑͬͯݺͿॲཧΛม͍͑ͨ •1.daysͷͱ͖ɿDateTime().addΛݺͼ͍ͨ •1.monthsͷͱ͖ɿDateTime().addMonthΛݺͼ͍ͨ •Duration͸microsecondsͷ֓೦ɺ୯Ґ͸͍࣋ͬͯͳ͍ •ੜ੒࣌ʹdaysͱmonthsͷͲͪΒ͕౉͞Ε͔ͨΘ͔Βͳ͍

  24. •DuraionʹtypeΛ࣋ͨͤΕ͹͍͍ͷͰ͸ extension ExtensionSupporterDuration on Duration { DurationType type; } •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍

    •(Ϋϥεม਺͸௥ՃͰ͖Δ) DateTime() + 1.month
  25. •ͳͷͰ͜͏ͨ͠ class DurationMonth extends Duration { int months; DurationMonth({this.months =

    0}) : super(); } extension ExtensionSupporterNum on num { DurationMonth get months => DurationMonth(months: toInt()); } DateTime() + 1.month
  26. •ͳͷͰ͜͏ͨ͠ extension ExtensionSupporterDateTime on DateTime { DateTime operator +(DurationMonth duration)

    { if (duration.runtimeType == DurationMonth) { return addMonth(duration.months); } return add(duration); } } IUUQTHJUIVCDPNNBLJDBNFMFYUFOTJPO@TVQQPSUFS ͥͻΞυόΠε͍ͩ͘͞ DateTime() + 1.month
  27. ϋϚΓͲ͜Ζ •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍ •NNBD(Non-Null by Default; null҆શ)
 Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍ •ϝιου໊ͷিಥʹ஫ҙ

  28. NNBD •௕͍ •ಉ͜͡ͱԿճ΋ॻ͖ͨ͘ͳ͍ if (user.containsKey('someList') && user['someList'].isNotEmpty()) { ... }

  29. NNBD extension MyNull on Null { bool get isEmpty =>

    true; } null.isEmpty; // => true final user = {'name': 'makicamel'}; user['someList'].isEmpty; // Unhandled exception: // NoSuchMethodError: // The getter 'isEmpty' was called on null.
  30. NNBD where a legacy unsafely nullable type may 
 contain

    null ,
 it is a run-time error if a migrated extension with a 
 non-nullable on type is called on null %BSU4UBUJD&YUFOTJPO.FUIPET%FTJHOEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFCMPCFEDDCEBD⒎DD BDDFQUFEGVUVSFSFMFBTFTTUBUJDFYUFOTJPONFUIPETGFBUVSFTQFDJpDBUJPONE ैདྷͷnullableͳܕʹnullؚ͕·ΕΔ৔߹ɺ
 nullෆՄͷܕͷExtension͕nullͰݺ͹ΕΔͱ
 ࣮ߦ࣌ΤϥʔʹͳΓ·͢ &YUFOEͰ͖ͳ͍
  31. ϋϚΓͲ͜Ζ •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍ •NNBD(Non-Null by Default; null҆શ)
 Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍ •ϝιου໊ͷিಥʹ஫ҙ

  32. ϝιου໊ͷিಥ extension ListT<T> on List<int> { void best() => print('bestT');

    } extension ListInt<int> on List<int> { void best() => print('bestInt'); } ίϯύΠϧΤϥʔʹͳΒͳ͍
 ʢͪͳΈʹ'bestT'͕ฦͬͯ͘Δ)
  33. ϝιου໊ͷিಥ extension ListT<T> on List<int> { void best() => print('bestT');

    } extension ListNum<num> on List<int> { void best() => print('bestNum'); } // => Error: The method 'best' isn't defined // for the class 'List<int>'. ৄࡉ౓͕ಉ͡Extension͕ఆٛ͞Ε͍ͯΔͱ
 ίϯύΠϧΤϥʔʹͳΔ
  34. ϝιου໊ͷিಥ <int>[1].best(); extension ListInt<T> on List<int> { void best() =>

    print('bestInt'); } extension ListT<T> on List<T> { void best() => print('bestT'); } extension ListNum<T> on List<num> { void best() => print('bestNum'); } extension ListNone<T> on List { void best() => print('bestNone'); } ͨ͠৔߹ɺ্͕ڧ͍ˡˠԼ͕ऑ͍
  35. •ৄࡉ౓Λߟ͑࢝ΊΔઃܭ͸Կ͔͕ؒҧ͍ͬͯΔ ϝιου໊ͷিಥ

  36. ͭ·Γ

  37. •૝ఆͨ͠ڍಈͱҧ͏ڍಈΛ͢ΔՄೳੑ͕͋Δ •ؒ઀తʹ֦ு͞Ε͍ͯΔͱมߋʹؾ͖ͮʹ͍͘ •໊લ͕িಥͨ࣌͠ʹยํ͕༏ઌ౓ͷ໰୊Ͱෛ͚Δ •໊લ͕িಥͨ࣌͠ʹ༏ઌ౓͕ಉ͡ͰΤϥʔʹͳΔ •ؒ઀తʹ֦ு͞Ε͍ͯΔͱিಥʹؾ͖ͮʹ͍͘ •ϥΠϒϥϦಉ࢜Ͱ֦ு͕িಥ͢Δͱղܾ͕ࠔ೉ •☝Λௐ੔͠Α͏ͱͯ͠ͳίʔυ͕ੜ·Ε΍͍͢ Ϋϥεͷڍಈͷมߋ͸ةݥ

  38. •ΞϓϦέʔγϣϯίʔυͰͷར༻͸ϧʔϧܾΊ͕େࣄ •extensionsσΟϨΫτϦΛ੾ִͬͯ཭ •ϑΝΠϧΛࡉ͔͘෼͚ͯখ͘͞importͰ͖ΔΑ͏ʹ͢Δ •APIΛ૿΍͢৔߹͸νʔϜ಺Ͱ໋໊نଇΛઃఆ •( ͔ͿΓͦ͏ͳ໊લ͸࣌ݶര஄ ) •ϥΠϒϥϦͷόάͳͲҰ࣌తͳύονͷ৔߹͸APIΛมߋ ͠ͳ͍ •ͦ΋ͦ΋ແҋʹߦ͏΋ͷͰ͸ͳ͍

    Ϋϥ εͷڍಈͷมߋ͸ةݥʁ
  39. ϥΠϒϥϦ࡞੒࣌ͷํ਑ •֦ுͨ͠ϝιουΛAPIͱͯ͠ఏڙ͠ͳ͍৔߹ •ϝιου໊ͷϓϨϑΟοΫεΛύοέʔδ໊ʹ͢ΔͳͲ ͷϧʔϧ͕͋Δͱ҆৺ͦ͏ •֦ுͨ͠APIΛఏڙ͢Δ৔߹ DateTime.now() + 1.months; •ϝιου໊ࣗମʹҙຯ͕͋Δ •ϑΝΠϧΛࡉ͔͘෼Θ͚Δ͘Β͍ͷରࡦ͔ͱ…

  40. •஫ҙ఺ •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍ •null҆શͰͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍ •ϝιου໊ͷিಥʹ஫ҙ •Static Extension Methods͸͍͍ͧ •௒؆୯ʹ࢝ΊΒΕΔ •ඪ४Ϋϥεʹϝιου௥ՃͰ͖Δͷָ͍͠ʂ ·ͱΊ

  41. ༻๏ɾ༻ྔΛकͬͯ ͝ར༻͸ܭըతʹ ͨͩ͠

  42. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠