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言語仕様の整理の年といえる

notoroid

June 27, 2022
Tweet

More Decks by notoroid

Other Decks in Programming

Transcript

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

    3FOFXBM w 6*1BTUF$POUSPM w 6*&EJU.FOV*OUFSBDUJPO IUUQTUXJUUFSDPNOPCJTUBUVTQIPUP
  2. // લఏ 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ʙ
  3. // લఏ 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ʙ
  4. 4XJGUݴޠܹಈͷ̐೥ؒ "QQMF͕4XJGU6*ඞཁͳ࢓༷Λແཧ΍Γݴޠ࢓༷ʹ௨ͨ͠ w 88%$ w 4XJGU6* TPNF  w 88%$

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

    SF ͕࡞ΒΕͨ w 88%$͔ΒৼΓฦΔͱ  w 0CKFDUJWF$Λஔ͖׵͑Δͷ͸খ͞ͳ͜ͱ w 4XJGU6*΍"QQ*OUFOUTɺ8JEHFU,JUͱ͍ͬͨ໺৺తͳػೳΛ࠷ॳ͔Βߟ͑ ͍ͯͨ
  6. 4XJGUݴޠ͕ରԠ͖ͯͨ͠՝୊ ୡ੒ॱͰιʔτ  σʔλແޮ OJM ݕ஌ͷపఈ w   HVBSE

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

     աڈࢿ࢈ GSBNFXPSL ͷ׆༻ w"OZ !PCK "OZ0CKFDU  Ϗϧυ࣌ʹΑΔݫີͳܕνΣοΫػߏ wݴޠ࢓༷  ۩ମతͳ࣮૷Λԡ͠ࠐΈ PSӅṭ͢Δ  w1SPQFSUZ8SBQQFS HFOFSJD 4XJGU΁ͷ࢓༷௥Ճ͸ɺ Ҏલ
  8. ۩ମྫ4XJGUHFOFSJDTᶃ 4XJGUͷ.BUIBCT ˡઈର஋ΛಘΔ @inlinable public func abs<T>(_ x: T) ->

    T where T : Comparable, T : SignedNumeric ઈର஋Λม͑Δͱ͍͏zΞΠσΞz ఆٛΛݟͯΈΔͱɺ۩ମతͳܕ͸໌ࣔ͞Ε͍ͯͳ͍ ΞΠσΞΛಛఆͷৄࡉ͔Β෼཭͞Ε͍ͯΔͱ͍͑Δ
  9. ۩ମྫ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ͷ Ҿ਺ͷܕΛܾఆ ܕਪ࿦
  10. ۩ମྫ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)") γϯϓϧͳྫͰ͸໰୊ͳ͍ɻ
  11. ۩ମྫ4XJGUHFOFSJDTᶆ ࣮ࡍʹ࢖༻͢Δࡍʹ͸՝୊͕ग़ͯ͘Δ parameter result parameter parameter parameter parameter parameter parameter

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

    result result result result result result 􀫥 ܕਪ࿦࣍ୈͰ ओʹQSPUPDPM ɺ ίϯύΠϧ࣌ͷܭࢉ͕രൃతʹ૿͑Δ ˞4XJGUͷ࢓্༷ճආͰ͖ͳ͍
  13. ࢀߟ 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