Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Carefully use and Enjoy Static Extension Methods

makicamel
November 07, 2019

Carefully use and Enjoy Static Extension Methods

makicamel

November 07, 2019
Tweet

More Decks by makicamel

Other Decks in Programming

Transcript

  1. 4UBUJD&YUFOTJPO.FUIPET

    ࢖͏લʹ஌͓͖͍ͬͯͨ஫ҙॻ͖
    2019.11.7.
    Flutter Meetup Tokyo #12
    @makicamel

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. Dart 2.6 is available
    IUUQTEBSUEFW

    View full-size slide

  5. IUUQTUXJUUFSDPNEBSU@MBOHTUBUVT

    View full-size slide

  6. Static Extension Methods
    %BSU4UBUJD&YUFOTJPO.FUIPET%FTJHOEBSUMBOHMBOHVBHFc(JU)VC

    IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFCMPCFEDDCEBD⒎DD
    BDDFQUFEGVUVSFSFMFBTFTTUBUJDFYUFOTJPONFUIPETGFBUVSFTQFDJpDBUJPONE

    View full-size slide

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

    View full-size slide

  8. DateFormat('yyyy/MM/dd').
    format(DateTime.now().add(Duration(days: 1)));
    ͜Ε·Ͱ
    •௕͍
    •ͱ͘ʹ3͕ಡΈͮΒ͍
    ᶃ ᶄ

    ɹ໌೔ͷ೔෇ͷจࣈྻΛऔಘ͍ͨ͠

    View full-size slide

  9. doMyOtherStuff(doMyStuff(obj.doStuff()).doOtherStuff());


    ͜Ε·Ͱ




    View full-size slide

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

    View full-size slide

  11. obj.doStuff().doMyStuff().doOtherStuff().doMyOtherStuff();

    ͜Ε͔Β

    View full-size slide

  12. 4UBUJD&YUFOTJPO5ZQFTEBSUMBOHMBOHVBHFc(JU)VC

    IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT
    Static Extension Methods
    •Readability

    (Մಡੑ)
    •Discoverability(IDE Support)

    (ൃݟ༰қੑ(IDEͷαδΣετΛड͚΍͍͢))
    • Static Extension Types
    • Pipeline-operator
    ɹ΋ݕ౼த
    1JQFMJOFPQFSBUPSGPSJOMJOFJOWPDBUJPOPGTUBUJDGVODUJPOTEBSUMBOHMBOHVBHFc(JU)VC

    IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT

    View full-size slide

  13. How to try it
    IUUQTUXJUUFSDPN@NPOPTUBUVT
    stableҎ֎Ͱ2.6.0Ҏ্ͷਖ਼ࣜ൛͕ೖΓ·ͨ͠

    View full-size slide

  14. 1. flutter channel
    ɹࣗ෼ͷνϟϯωϧΛ֬ೝ

    3 steps to try it
    ❯ flutter channel
    Flutter channels:
    beta
    dev
    master
    * stable
    ͠͹Β͘upgrade͍ͯ͠ͳ͍ਓ͸
    flutter upgrade ΋
    flutter upgrade

    View full-size slide

  15. 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'

    View full-size slide

  16. 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
    ४උ׬ྃ

    View full-size slide

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

    View full-size slide

  18. extension ExtensionSupporterNum on Num {
    Duration get days =>
    milliseconds * Duration.millisecondsPerDay;
    }
    1.days

    View full-size slide

  19. 1.days
    1.days
    // => 24:00:00.000000

    View full-size slide

  20. ϋϚΓͲ͜Ζ
    •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍
    •NNBD(Non-Null by Default; null҆શ)

    Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍
    •ϝιου໊ͷিಥʹ஫ҙ

    View full-size slide

  21. ϋϚΓͲ͜Ζ
    •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍
    •NNBD(Non-Null by Default; null҆શ)

    Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍
    •ϝιου໊ͷিಥʹ஫ҙ

    View full-size slide

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

    View full-size slide

  23. •DuraionʹtypeΛ࣋ͨͤΕ͹͍͍ͷͰ͸
    extension ExtensionSupporterDuration on Duration {
    DurationType type;
    }
    •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍
    •(Ϋϥεม਺͸௥ՃͰ͖Δ)
    DateTime() + 1.month

    View full-size slide

  24. •ͳͷͰ͜͏ͨ͠
    class DurationMonth extends Duration {
    int months;
    DurationMonth({this.months = 0}) : super();
    }
    extension ExtensionSupporterNum on num {
    DurationMonth get months =>
    DurationMonth(months: toInt());
    }
    DateTime() + 1.month

    View full-size slide

  25. •ͳͷͰ͜͏ͨ͠
    extension ExtensionSupporterDateTime on DateTime {
    DateTime operator +(DurationMonth duration) {
    if (duration.runtimeType == DurationMonth) {
    return addMonth(duration.months);
    }
    return add(duration);
    }
    }
    IUUQTHJUIVCDPNNBLJDBNFMFYUFOTJPO@TVQQPSUFS
    ͥͻΞυόΠε͍ͩ͘͞
    DateTime() + 1.month

    View full-size slide

  26. ϋϚΓͲ͜Ζ
    •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍
    •NNBD(Non-Null by Default; null҆શ)

    Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍
    •ϝιου໊ͷিಥʹ஫ҙ

    View full-size slide

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

    View full-size slide

  28. 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.

    View full-size slide

  29. 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Ͱ͖ͳ͍

    View full-size slide

  30. ϋϚΓͲ͜Ζ
    •Πϯελϯεม਺͸௥ՃͰ͖ͳ͍
    •NNBD(Non-Null by Default; null҆શ)

    Ͱͳ͍ม਺ͷnull͸֦ுͰ͖ͳ͍
    •ϝιου໊ͷিಥʹ஫ҙ

    View full-size slide

  31. ϝιου໊ͷিಥ
    extension ListT on List {
    void best() => print('bestT');
    }
    extension ListInt on List {
    void best() => print('bestInt');
    }
    ίϯύΠϧΤϥʔʹͳΒͳ͍

    ʢͪͳΈʹ'bestT'͕ฦͬͯ͘Δ)

    View full-size slide

  32. ϝιου໊ͷিಥ
    extension ListT on List {
    void best() => print('bestT');
    }
    extension ListNum on List {
    void best() => print('bestNum');
    }
    // => Error: The method 'best' isn't defined
    // for the class 'List'.
    ৄࡉ౓͕ಉ͡Extension͕ఆٛ͞Ε͍ͯΔͱ

    ίϯύΠϧΤϥʔʹͳΔ

    View full-size slide

  33. ϝιου໊ͷিಥ
    [1].best();
    extension ListInt on List {
    void best() => print('bestInt');
    }
    extension ListT on List {
    void best() => print('bestT');
    }
    extension ListNum on List {
    void best() => print('bestNum');
    }
    extension ListNone on List {
    void best() => print('bestNone');
    }
    ͨ͠৔߹ɺ্͕ڧ͍ˡˠԼ͕ऑ͍

    View full-size slide

  34. •ৄࡉ౓Λߟ͑࢝ΊΔઃܭ͸Կ͔͕ؒҧ͍ͬͯΔ
    ϝιου໊ͷিಥ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide