Share
Embed
Carefully use and Enjoy Static Extension Methods
makicamel
November 07, 2019
Transcript
42 slides
-
4UBUJD&YUFOTJPO5ZQFTEBSUMBOHMBOHVBHFc(JU)VC IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT Static Extension Methods •Readability (Մಡੑ) •Discoverability(IDE Support) (ൃݟ༰қੑ(IDEͷαδΣετΛड͚͍͢))
• Static Extension Types • Pipeline-operator ɹݕ౼த 1JQFMJOFPQFSBUPSGPSJOMJOFJOWPDBUJPOPGTUBUJDGVODUJPOTEBSUMBOHMBOHVBHFc(JU)VC IUUQTHJUIVCDPNEBSUMBOHMBOHVBHFJTTVFT -
1. flutter channel ɹࣗͷνϟϯωϧΛ֬ೝ 3 steps to try it
❯ flutter channel Flutter channels: beta dev master * stable ͠Β͘upgrade͍ͯ͠ͳ͍ਓ flutter upgrade flutter upgrade -
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' -
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 ४උྃ -
Static Extension Methodsͷߏจ extension ExtensionSupporterNum on Num { Duration get
days => milliseconds * Duration.millisecondsPerDay; } Ϋϥε໊ ֦ுΫϥε໊࣮ࡍΘͳ͍ͷͰ িಥ͠ʹ໊͍͘લʹ͢Δ -
extension ExtensionSupporterNum on Num { Duration get days => milliseconds
* Duration.millisecondsPerDay; } 1.days -
•DuraionʹtypeΛ࣋ͨͤΕ͍͍ͷͰ extension ExtensionSupporterDuration on Duration { DurationType type; } •ΠϯελϯεมՃͰ͖ͳ͍
•(ΫϥεมՃͰ͖Δ) DateTime() + 1.month -
•ͳͷͰ͜͏ͨ͠ class DurationMonth extends Duration { int months; DurationMonth({this.months =
0}) : super(); } extension ExtensionSupporterNum on num { DurationMonth get months => DurationMonth(months: toInt()); } DateTime() + 1.month -
•ͳͷͰ͜͏ͨ͠ extension ExtensionSupporterDateTime on DateTime { DateTime operator +(DurationMonth duration)
{ if (duration.runtimeType == DurationMonth) { return addMonth(duration.months); } return add(duration); } } IUUQTHJUIVCDPNNBLJDBNFMFYUFOTJPO@TVQQPSUFS ͥͻΞυόΠε͍ͩ͘͞ DateTime() + 1.month -
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. -
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Ͱ͖ͳ͍ -
ϝιου໊ͷিಥ extension ListT<T> on List<int> { void best() => print('bestT');
} extension ListInt<int> on List<int> { void best() => print('bestInt'); } ίϯύΠϧΤϥʔʹͳΒͳ͍ ʢͪͳΈʹ'bestT'͕ฦͬͯ͘Δ) -
ϝιου໊ͷিಥ 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͕ఆٛ͞Ε͍ͯΔͱ ίϯύΠϧΤϥʔʹͳΔ -
ϝιου໊ͷিಥ <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'); } ͨ͠߹ɺ্͕ڧ͍ˡˠԼ͕ऑ͍