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

整理されたSwift言語仕様 - Swift 5.7 any/some

整理されたSwift言語仕様 - Swift 5.7 any/some

anyに巡りつくまでの経緯を探る必要がある。

Swift言語 激動の4年間
AppleがSwiftUI必要な仕様を無理やり言語仕様に通した

WWDC19 5.1
SwiftUI(some)
WWDC20 5.3
PropertyWrapper(Swift Attributes)
WWDC21
5.5 async/await/actor
WWDC22 5.7
any/some

傾向としては、WWDC22ではSwift言語仕様の整理の年といえる

0fb0b8c6eecbc8e0a3981501f6a6fea6?s=128

notoroid

June 27, 2022
Tweet

More Decks by notoroid

Other Decks in Technology

Transcript

  1. ೳొཁ !OPUPSPJE J1IPOFΞϓϦ։ൃऀ೥݄೔ ੔ཧ͞Εͨ4XJGUݴޠ࢓༷ 4XJGU

  2. ࿩͢಺༰ w 4XJGUͰ੔ཧ͞Εͨจ๏ w ੔ཧʹࢸΔ·Ͱͷ4XJGUݴޠ

  3. ࿩͞ͳ͍಺༰ w TPNFͷৄࡉ w ଞͷ௥Ճ࢓༷ 4XJGUDPODVSSFODZ

  4. ͓୊໨ w 88%$ऴྃ͠·ͨ͠ w 4XJGUݴޠͷܦҢ w ·ͱΊ

  5. 88%$ऴྃ͠·ͨ͠

  6. 88%$ॾʑ৽ػೳ΍ػೳͷϦχϡʔΞϧ͕ਐΉ w /FX w 4XJGU$IBSU w 8FBUIFS,JU w 6*$BMFOEBS7JFX w

    3FOFXBM w 6*1BTUF$POUSPM w 6*&EJU.FOV*OUFSBDUJPO IUUQTUXJUUFSDPNOPCJTUBUVTQIPUP
  7. (PPEOFXT

  8. 4XJGUݴޠͷ࢓༷͕௥Ճ هड़ํ๏͕੔ཧ͞Ε·ͨ͠

  9. 4XJGUͷ࢓༷௥Ճ w 4XJGUHFOFSJD w ϝιουʹ౉͢Ҿ਺΍໭Γ஋ͷQSPSPDPMࢦఆํ๏͕؆ܿʹͳΓ·ͨ͠ w ؆ܿʹهड़͢ΔͨΊʹBOZ͕ಋೖ͞Ε·ͨ͠ w େจࣈͷ"OZ 0CKFDUJWF$ͷJE

    ͱ͸ҟͳΔ
  10. // લఏ protocol FooProtocol { } struct ImplementFoo: FooProtocol{ }

    struct AnotherFoo: FooProtocol{ } 4XJGUͷ࢓༷௥Ճ func fooFunc<T: FooProtocol>(_ object: T) { } func fooFunc(_ object: any FooProtocol) { } ˡQSPUPDPM ˡQSPUPDPMΛ࣮૷͢ΔTUSVDU ෆཁʹ ෆཁʹ 4XJGUʙ 4XJGUʙ
  11. // લఏ protocol FooProtocol { } struct ImplementFoo: FooProtocol{ }

    struct AnotherFoo: FooProtocol{ } 4XJGUͷ࢓༷௥Ճ struct Sample { func fooMethod<T: FooProtocol>(_ object: T) { } } struct Sample2 { func fooMethod(_ object: any FooProtocol) { } } ˡQSPUPDPM ˡQSPUPDPMΛ࣮૷͢ΔTUSVDU ෆཁʹ ෆཁʹ 4XJGUʙ 4XJGUʙ
  12. γϯϓϧʹͳͬͯྑ͔ͬͨͶʂ

  13. Ҏ্ʂ

  14. ͦΜͳΘ͚͸ͳ͘

  15. BOZʹ८Γͭ͘·ͰͷܦҢΛ ୳Δඞཁ͕͋Δ

  16. 4XJGUݴޠͷBOZಋೖ·ͰͷܦҢ

  17. ·ͣ͸88%$ͷ ηογϣϯ঺հ

  18. 88%$4XJGUؔ࿈ॏཁηογϣϯ w &NCSBDF4XJGUHFOFSJDT88%$7JEFPT"QQMF%FWFMPQFS w IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED w %FTJHOQSPUPDPMJOUFSGBDFTJO4XJGU88%$7JEFPT"QQMF %FWFMPQFS w IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED

    88%$ 88%$
  19. 4XJGUݴޠܹಈͷ̐೥ؒ

  20. 4XJGUݴޠܹಈͷ̐೥ؒ "QQMF͕4XJGU6*ඞཁͳ࢓༷Λແཧ΍Γݴޠ࢓༷ʹ௨ͨ͠ w 88%$ w 4XJGU6* TPNF  w 88%$

    w 1SPQFSUZ8SBQQFS 4XJGU"UUSJCVUFT  w 88%$ w BTZODBXBJUBDUPS w 88%$ w BOZTPNF ܏޲ͱͯ͠͸ɺ 88%$Ͱ͸4XJGUݴޠ ࢓༷ͷ੔ཧͷ೥ͱ͍͑Δ
  21. 4XJGUݴޠܹಈͷ̐೥ؒ 4XJGU͸ϓϥοτϑΥʔϜ։ൃऀ޲͚ʹ࡞ͬͨݴޠ w ΞϓϦ։ൃऀίϛϡχςΟ͸0CKFDUJWF$Λஔ͖׵͑Δݴޠͱͯ͠4XJGUΛ޷ ҙతʹܴ͑ೖΕͨ w 4XJGUެ։ޙఆ൪αʔυύʔςΟϥΠϒϥϦ 3Y4XJGU "MBNP fi

    SF ͕࡞ΒΕͨ w 88%$͔ΒৼΓฦΔͱ  w 0CKFDUJWF$Λஔ͖׵͑Δͷ͸খ͞ͳ͜ͱ w 4XJGU6*΍"QQ*OUFOUTɺ8JEHFU,JUͱ͍ͬͨ໺৺తͳػೳΛ࠷ॳ͔Βߟ͑ ͍ͯͨ
  22. 4XJGUݴޠ͕ରԠ͖ͯͨ͠՝୊

  23. 4XJGUݴޠ͕ରԠ͖ͯͨ͠՝୊ ୡ੒ॱͰιʔτ  σʔλແޮ OJM ݕ஌ͷపఈ w   HVBSE

     աڈࢿ࢈ GSBNFXPSL ͷ׆༻ w"OZ !PCK "OZ0CKFDU  Ϗϧυ࣌ʹΑΔݫີͳܕνΣοΫػߏ wݴޠ࢓༷  ۩ମతͳ࣮૷Λԡ͠ࠐΈ PSӅṭ͢Δ  w1SPQFSUZ8SBQQFS HFOFSJD
  24. 4XJGUݴޠ͕ରԠ͖ͯͨ͠՝୊ ୡ੒ॱͰιʔτ  σʔλແޮ OJM ݕ஌ͷపఈ w   HVBSE

     աڈࢿ࢈ GSBNFXPSL ͷ׆༻ w"OZ !PCK "OZ0CKFDU  Ϗϧυ࣌ʹΑΔݫີͳܕνΣοΫػߏ wݴޠ࢓༷  ۩ମతͳ࣮૷Λԡ͠ࠐΈ PSӅṭ͢Δ  w1SPQFSUZ8SBQQFS HFOFSJD 4XJGU΁ͷ࢓༷௥Ճ͸ɺ Ҏલ
  25. ೥͔Β͸"QQMF༏ઌ Ͱ࢓༷͕Ͷ͡ࠐ·ΕΔ ΞϓϦ։ൃऀ͕ࢥ͍ඳ͍ͨ Ϟμϯͳݴޠ4XJGU͔Β "QQMF͕΍Γ͍ͨࣄΛ࣮ݱ ͢Δઌฌͱͳͬͨײ͕͋Δ

  26. 4XJGUHFOFSJDT

  27. 4XJGUHFOFSJDT w "QQMF͕Ӡ͏ʹ w 4XJGUͰந৅తͳίʔυΛॻͨ͘Ίͷجຊతͳπʔϧ w ίʔυ͕ਐԽ͢ΔʹͭΕͯෳࡶ͞Λ؅ཧ͢ΔͨΊʹෆՄܽ w ந৅Խ w

    ΞΠσΞΛಛఆͷৄࡉ͔Β෼཭͢Δ
  28. ۩ମྫ4XJGUHFOFSJDT

  29. ۩ମྫ4XJGUHFOFSJDTᶃ 4XJGUͷ.BUIBCT ˡઈର஋ΛಘΔ @inlinable public func abs<T>(_ x: T) ->

    T where T : Comparable, T : SignedNumeric ઈର஋Λม͑Δͱ͍͏zΞΠσΞz ఆٛΛݟͯΈΔͱɺ۩ମతͳܕ͸໌ࣔ͞Ε͍ͯͳ͍ ΞΠσΞΛಛఆͷৄࡉ͔Β෼཭͞Ε͍ͯΔͱ͍͑Δ
  30. ۩ମྫ4XJGUHFOFSJDTᶄ 4XJGUͷ.BUIBCT ˡઈର஋ΛಘΔ @inlinable public func Add<T>(_ x1: T, _

    x2: T) -> T where T : Comparable, T : SignedNumeric { x1 + x2 } let value1 = 11.0 let value2 = 3.0 let result = Add( abs(value1), value2) print("result=\(result)") ந৅Խ͞Εͨίʔυಉ࢜͸Ҿ਺ͱ໭Γ஋Ͱܕਪ࿦͞ΕΔ "TCͷ໭Γ஋͕ɺ"EEͷ Ҿ਺ͷܕΛܾఆ ܕਪ࿦
  31. ۩ମྫ4XJGUHFOFSJDTᶅ ࣮ࡍʹ࢖༻͢Δࡍʹ͸՝୊͕ग़ͯ͘Δ parameter result parameter result result @inlinable public func

    Add<T>(_ x1: T, _ x2: T) -> T where T : Comparable, T : SignedNumeric { x1 + x2 } let value1 = 11.0 let value2 = 3.0 let result = Add( abs(value1), value2) print("result=\(result)") γϯϓϧͳྫͰ͸໰୊ͳ͍ɻ
  32. ۩ମྫ4XJGUHFOFSJDTᶆ ࣮ࡍʹ࢖༻͢Δࡍʹ͸՝୊͕ग़ͯ͘Δ parameter result parameter parameter parameter parameter parameter parameter

    result result result result result result ܕ5 ܕ5Λ࢖ ͏ผܕ ܕ5Λ࢖͏ผܕΛ ࢖͏ผܕ ܕ5Λ࢖͏ผܕΛ࢖͏ผܕ Λ࢖͏ผܕ 4XJGU6*ɺ$PNCJOFɺ3Y4XJGUͳͲ Ͱ͸ܕΛ࢖ͬͨผܕʹม׵ଟ਺
  33. ۩ମྫ4XJGUHFOFSJDTᶇ ࣮ࡍʹ࢖༻͢Δࡍʹ͸՝୊͕ग़ͯ͘Δ parameter result parameter parameter parameter parameter parameter parameter

    result result result result result result 􀫥 ܕਪ࿦࣍ୈͰ ओʹQSPUPDPM ɺ ίϯύΠϧ࣌ͷܭࢉ͕രൃతʹ૿͑Δ ˞4XJGUͷ࢓্༷ճආͰ͖ͳ͍
  34. TPNFͷొ৔ 4XJGU6*ͱڞʹ௥Ճ͞Εͨ4JXGUݴޠͷ࢓༷ w HFOFSJDT࢖༻࣌ʹҾ਺΍໭Γ஋ʹରͯ͠෇Ճ͢Δ΋ͷ w ෇ՃͰ͖Δͷ͸Ҿ਺ɺ໭Γ஋͸Կ͔͠ΒͷQSPUPDPMܕ w QSPUPDPMܕΛҾ਺΍ม਺ͱͯ͠࢖͏ͱίετ͕ߴ͍ͨΊͷճආࡦ w Ϗϧυ࣌ʹԆʑͱ͕͔͔࣌ؒΔ

    w ࣮ߦ࣌ʹ΋Өڹେ͖͍ Β͍͠
  35. ͳͥBOZ͕88%$Ͱొ৔ w TPNFͷରͱͳΔݴޠ࢓༷͕ඞཁ͕ͩͬͨ4XJGU6*ൃද࣌͸TPNFΛ༏ઌ w 88%$ͷࡍ͸BTZODBXBJUBDUPSͰखҰഋ w 88%$Ͱ͸༨༟͕͋ͬͨ ͷͰ௥Ճ

  36. ·ͱΊ

  37. ·ͱΊ w 4XJGU6*ʹؒʹ߹ΘͤΔͨΊʹࠞཚ͍ͯͨ͠4XJGUݴޠ࢓༷͕੔ཧ͞Ε·ͨ͠ w ೥ͷ4XJGU6*ެ։Ҏ߱ɺ৽ػೳͱ੔ཧύλʔϯ͕ݟड͚ΒΕ·͢ w 4XJGUͷQSPUPUZQFͷ࢓༷ͱੑೳվળΛՌͨͨ͢ΊTPNF͕ඞཁͰͨ͠ w TPNFͱର ͍ͭ

    ͱͳΔܗͰBOZ͕௥Ճ͞Ε·ͨ͠
  38. J045FDI#MPHߋ৽த w4XJDU6*Λ࢖ͬͯ̍̌̌ߦͰαϯϓϧίʔυΛॻ͘ 4XJGUߦγϦʔζଞ"QQMF։ൃؔ܎ͷهࣄΛܝࡌத Ͱ͢ɻ JSJNBTVDPN OPUFDPNOPUPSPJE wʹ΋هࡌதͰ͢ɻ

  39. ࢀߟ w &NCSBDF4XJGUHFOFSJDT88%$7JEFPT"QQMF%FWFMPQFS w IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED w %FTJHOQSPUPDPMJOUFSGBDFTJO4XJGU88%$7JEFPT"QQMF%FWFMPQFS w IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED w

    4XJGUʹಋೖ͞ΕΔ0QBRVF3FTVMU5ZQFͱ͸Կ͔2JJUB w IUUQTRJJUBDPNLPIFSJUFNTEGEDFG w <ϓϨθϯࢿྉ> "QQMF͕ ΍Γ͍ͨ์୊4XJGU"UUSJCVUFT w IUUQTJSJNBTVDPNBQQMFTXJGUBUUSJCVUFT