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&YUFOTJPO5ZQFTEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT Static Extension Methods •Readability
 (Մಡੑ) •Discoverability(IDE Support)
 (ൃݟ༰қੑ(IDEͷαδΣετΛड͚΍͍͢))

    • Static Extension Types • Pipeline-operator ɹ΋ݕ౼த 1JQFMJOFPQFSBUPSGPSJOMJOFJOWPDBUJPOPGTUBUJDGVODUJPOTEBSUMBOHMBOHVBHFc(JU)VC
 IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT
  2. 1. flutter channel ɹࣗ෼ͷνϟϯωϧΛ֬ೝ 
 3 steps to try it

    ❯ flutter channel Flutter channels: beta dev master * stable ͠͹Β͘upgrade͍ͯ͠ͳ͍ਓ͸ flutter upgrade ΋ flutter upgrade
  3. 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'
  4. 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 ४උ׬ྃ
  5. Static Extension Methodsͷߏจ extension ExtensionSupporterNum on Num { Duration get

    days => milliseconds * Duration.millisecondsPerDay; } Ϋϥε໊ ֦ுΫϥε໊͸࣮ࡍ࢖Θͳ͍ͷͰ িಥ͠ʹ໊͍͘લʹ͢Δ
  6. •ͳͷͰ͜͏ͨ͠ class DurationMonth extends Duration { int months; DurationMonth({this.months =

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

    { if (duration.runtimeType == DurationMonth) { return addMonth(duration.months); } return add(duration); } } IUUQTHJUIVCDPNNBLJDBNFMFYUFOTJPO@TVQQPSUFS ͥͻΞυόΠε͍ͩ͘͞ DateTime() + 1.month
  8. 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.
  9. 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Ͱ͖ͳ͍
  10. ϝιου໊ͷিಥ extension ListT<T> on List<int> { void best() => print('bestT');

    } extension ListInt<int> on List<int> { void best() => print('bestInt'); } ίϯύΠϧΤϥʔʹͳΒͳ͍
 ʢͪͳΈʹ'bestT'͕ฦͬͯ͘Δ)
  11. ϝιου໊ͷিಥ 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͕ఆٛ͞Ε͍ͯΔͱ
 ίϯύΠϧΤϥʔʹͳΔ
  12. ϝιου໊ͷিಥ <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'); } ͨ͠৔߹ɺ্͕ڧ͍ˡˠԼ͕ऑ͍