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

よせあつめSwift - Swift5.9

よせあつめSwift - Swift5.9

WWDC23(Appleの開発者向けカンファレンス) で発表されたプログラミング言語Swift5.9を新機能を中心に紹介。

notoroid

June 28, 2023
Tweet

More Decks by notoroid

Other Decks in Programming

Transcript

  1. Αͤ͋ͭΊ4XJGU
    4XJGU
    ೳొཁ !OPUPSPJE
    J1IPOFΞϓϦ։ൃऀ೥݄೔

    View full-size slide

  2. ࿩͢಺༰
    w 4XJGUͷ৽ػೳ
    w 4XJGU.BDSP͸৽ػೳ
    w 0CTFSWBUJPOGSBNFXPSL

    View full-size slide

  3. ࿩͞ͳ͍಺༰
    w ฒߦੑ $PODVSSFODZ
    ͷػೳڧԽ
    w 5BTL(SPVQͷछྨ௥Ճ ݁ՌΛอ࣋͠ͳ͍άϧʔϓԽ͞ΕͨλεΫ

    w λεΫͷ༏ઌ౓্֨͛ػೳ
    w λεΫ಺ͷ؀ڥม਺ڞ༗ػೳ
    w ฒߦੑ࣌ͷಈ࡞τϨʔε

    View full-size slide

  4. ͓୊໨
    w վળ͢͢Ή4XJGUݴޠ
    w ৽ػೳᶃʙᶅ
    w ৽ػೳ 4XJGUNBDSPT
    w 0CTFSWBUJPOͷαϯϓϧ
    w ·ͱΊ

    View full-size slide

  5. վળ͢͢Ή4XJGUݴޠ

    View full-size slide

  6. 88%$ऴΘΓ·ͨ͠Ͷ

    View full-size slide

  7. ڈ೥͸4XJGUͷ଍
    Ի͕ฉ͍͑ͯͨ͜͸
    ͕ͣͩʜ
    IUUQTEFWFMPQFSBQQMFDPN
    XXED

    View full-size slide

  8. 4XJGUݴޠ͸ΑΓ͔͸
    ৽ػೳ௥Ճ͸௿ௐ

    View full-size slide

  9. 8IBU`TOFXJO4XJGU88%$
    IUUQTEFWFMPQFSBQQMFDPNXXED

    View full-size slide

  10. 8IBU`TOFXJO4XJGU88%$
    IUUQTEFWFMPQFSBQQMFDPNXXED
    ϓϩδΣΫτશମͷऔΓ૊Έ
    ৽ػೳᶃJGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ
    ৽ػೳᶄܕύϥϝʔλʔͷύοέʔδରԠ
    4XJGUͷੑೳ໘޲্ͷ࿩
    ৽ػೳ 4XJGU"UUSJCVUFT͕ఏڙ͍ͯͨ͠ػೳΛϓ
    ϥάΠϯͱͯ͠ར༻Մೳʹ
    ੑೳ໘޲্ͨ͠4XJGUΛ࢖ͬͯϑϨʔϜϫʔΫΛ࡮৽த
    ৽ػೳᶅDMBTTͰͰ͖Δ͜ͱΛTUSVDUʹ೚͍ͤͨ
    ৽ػೳᶇฒߦੑͷॊೈੑΛڧԽ
    ˢͷ࠾༻ྫ
    ৽ػೳᶆ$޲͚ͷίʔυδΣωϨʔλʔ

    View full-size slide

  11. 8IBU`TOFXJO4XJGU88%$
    IUUQTEFWFMPQFSBQQMFDPNXXED
    ৽ػೳᶃJGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ
    ৽ػೳᶄܕύϥϝʔλʔͷύοέʔδରԠ
    ৽ػೳ 4XJGU"UUSJCVUFT͕ఏڙ͍ͯͨ͠ػೳΛϓ
    ϥάΠϯͱͯ͠ར༻Մೳʹ
    ৽ػೳᶅDMBTTͰͰ͖Δ͜ͱΛTUSVDUʹ೚͍ͤͨ
    ৽ػೳᶇฒߦੑͷॊೈੑΛڧԽ
    ৽ػೳᶆ$޲͚ͷίʔυδΣωϨʔλʔ

    View full-size slide

  12. 8IBU`TOFXJO4XJGU88%$
    IUUQTEFWFMPQFSBQQMFDPNXXED
    ৽ػೳᶃJGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ
    ৽ػೳᶄܕύϥϝʔλʔͷύοέʔδରԠ
    ৽ػೳ 4XJGU"UUSJCVUFT͕ఏڙ͍ͯͨ͠ػೳΛϓ
    ϥάΠϯͱͯ͠ར༻Մೳʹ
    ৽ػೳᶅDMBTTͰͰ͖Δ͜ͱΛTUSVDUʹ೚͍ͤͨ
    ৽ػೳᶇฒߦੑͷॊೈੑΛڧԽ
    ৽ػೳᶆ$޲͚ͷίʔυδΣωϨʔλʔ

    View full-size slide

  13. 8IBU`TOFXJO4XJGU88%$
    IUUQTEFWFMPQFSBQQMFDPNXXED
    ৽ػೳᶃJGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ
    ৽ػೳᶄܕύϥϝʔλʔͷύοέʔδରԠ
    ৽ػೳ 4XJGU"UUSJCVUFT͕ఏڙ͍ͯͨ͠ػೳΛϓ
    ϥάΠϯͱͯ͠ར༻Մೳʹ
    ৽ػೳᶅDMBTTͰͰ͖Δ͜ͱΛTUSVDUʹ೚͍ͤͨ
    ৽ػೳᶇฒߦੑͷॊೈੑΛڧԽ
    ৽ػೳᶆ$޲͚ͷίʔυδΣωϨʔλʔ
    ৽ػೳʙЋΛͬ͘͟Γઆ໌͍ͨ͠ɻ

    View full-size slide

  14. ৽ػೳᶃ
    JGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ

    View full-size slide

  15. 6TJOHJGFMTFBOETXJUDITUBUFNFOUTBTFYQSFTTJPOT
    JGFMTFTXJUDIεςʔτϝϯτΛࣜͱͯ͠ѻ͏͜ͱ͕Ͱ͖Δ
    enum Status: CaseIterable {

    case sleep

    case active

    case playingGame



    var description: String {

    switch self {

    case .active: return "ΞΫςΟϒ"

    case .sleep: return "ਭ຾"

    case .playingGame: return "ήʔϜத"

    }

    }

    }
    ྫFOVN4UBUVTʹઆ໌Λ෇Ճ͢ΔEFTDSJQUJPOଐੑΛ௥Ճ͢Δɻ

    View full-size slide

  16. 6TJOHJGFMTFBOETXJUDITUBUFNFOUTBTFYQSFTTJPOT
    JGFMTFTXJUDIεςʔτϝϯτΛࣜͱͯ͠ѻ͏͜ͱ͕Ͱ͖Δ
    enum Status: CaseIterable {

    case sleep

    case active

    case playingGame



    var description: String {

    return switch self {

    case .active: return "ΞΫςΟϒ"

    case .sleep: return "ਭ຾"

    case .playingGame: return "ήʔϜத"

    }

    }

    }
    ྫFOVN4UBUVTʹઆ໌Λ෇Ճ͢ΔEFTDSJQUJPOଐੑΛ௥Ճ͢Δɻ

    View full-size slide

  17. 6TJOHJGFMTFBOETXJUDITUBUFNFOUTBTFYQSFTTJPOT
    JGFMTFTXJUDIεςʔτϝϯτΛࣜͱͯ͠ѻ͏͜ͱ͕Ͱ͖Δ
    enum Status: CaseIterable {

    case sleep

    case active

    case playingGame



    var description: String {

    switch self {

    case .active: "ΞΫςΟϒ"

    case .sleep: "ਭ຾"

    case .playingGame:"ήʔϜத"

    }

    }

    }
    ྫFOVN4UBUVTʹઆ໌Λ෇Ճ͢ΔEFTDSJQUJPOଐੑΛ௥Ճ͢Δɻ
    4XJUDIจ͕จࣈྻΛฦࣜ͢ͱͯ͠ػೳ
    ͢Δؔ਺಺ʹ͔͕ͭࣜ͠ͳ͍ͷͰ
    SFUVSO΋লུͰ͖Δɻ

    View full-size slide

  18. ৽ػೳᶄ
    ܕύϥϝʔλʔͷύοέʔδରԠ

    View full-size slide

  19. UZQFQBSBNFUFSQBDLT
    ܕύϥϝʔλʔͷύοέʔδରԠ
    wͦ΋ͦ΋4XJGUͷHFOFSJD͸ΞΠσΞͱ࣮ࡍͷܕΛ෼཭͢Δख๏
    wෳ਺ͷܕΛҾ਺ͱͯ͠ΞΠσΞड͚औΔʹ͸Ҿ਺෼ͷఆ͕ٛඞཁͩͬͨ
    wෳ਺ͷܕΛ࣋ͭҾ਺ΛύοέʔδԽ͢Δ৽͍͠ఆ͕ٛొ৔
    wFBDIͱSFQFBUΛ࢖༻ͯ͠ܕύϥϝʔλఆٛΛ࣮ࡍͷ஋औΓग़͠Λ࣮ݱ
    FBDI3FTVMU

    View full-size slide

  20. ܕύϥϝʔλʔͷύοέʔδରԠᶃ
    func query(


    _ item: Request


    ) -> Payload


    func query(


    _ item1: Request,


    _ item2: Request


    ) -> (Payload1, Payload2)


    func query(


    _ item1: Request,


    _ item2: Request,


    _ item3: Request


    ) -> (Payload1, Payload2, Payload3)


    func query(


    _ item1: Request,


    _ item2: Request,


    _ item3: Request,


    _ item4: Request


    ) -> (Payload1, Payload2, Payload3, Payload4)


    View full-size slide

  21. func query(


    _ item: Request


    ) -> Payload


    func query(


    _ item1: Request,


    _ item2: Request


    ) -> (Payload1, Payload2)


    func query(


    _ item1: Request,


    _ item2: Request,


    _ item3: Request


    ) -> (Payload1, Payload2, Payload3)


    func query(


    _ item1: Request,


    _ item2: Request,


    _ item3: Request,


    _ item4: Request


    ) -> (Payload1, Payload2, Payload3, Payload4)


    func query(_ item: repeat RequestPayload>) -> (repeat each Payload)
    ύϥϝʔλ਺Λ૿΍ͨ͠ಉؔ͡਺ఆ͕ٛඞཁͩͬͨ
    ΋ͷ͕ͭͷύϥϝʔλʔͱͯ͠ѻ͏͜ͱ͕Մೳʹ
    ܕύϥϝʔλʔͷύοέʔδରԠᶄ

    View full-size slide

  22. w UZQFQBSBNFUFSQBDLT͸ύϥϝʔλʔͷܕΛอͬͨ··ͳͷͰؔ਺ͷ໭Γ஋
    ΁ɺܕͦͷ΋ͷɺܕʹؔ࿈͢ΔܕΛฦ͢͜ͱ͕Ͱ͖Δɻ
    ܕύϥϝʔλʔͷύοέʔδରԠᶅ
    w౉ͨ͠ύϥϝʔλʔΛ౉ͨ͠ύϥϝʔλʔ਺෼ͷλϓϧΛ
    ฦ͢͜ͱ͕Մೳɻ
    w໭Γ஋ʹύϥϝʔλͰ౉͞ΕͨܕΛ࢖Θͳ͍ͷͰ͋Ε͹ಛ
    ʹܕύϥϝʔλʔΛ࢖͏ඞཁ͕ͳ͍ɻ

    View full-size slide

  23. ܕύϥϝʔλʔͷύοέʔδରԠᶆ
    protocol Animal {


    associatedtype Food: AnimalFeed


    associatedtype Commodity: AnimalCommodity




    func eat(_ food: Food)


    func produce() -> Commodity


    }


    struct Chicken: Animal {


    typealias Food = Grain


    typealias Commodity = ChickenMeat


    func eat(_ food: Grain) {


    // do something eat...


    }




    func produce() -> ChickenMeat {


    return ChickenMeat()


    }


    }
    ܕͱ͍ͬͯ΋HFOFSJDTɺQSPUPDPMͱBTTPDJBUFEUZQFΛ
    ૊Έ߹ΘͤΔͱม׵Ͱ͖ΔܕͷόϦΤʔγϣϯ͸૿͑Δ

    View full-size slide

  24. w UZQFQBSBNFUFSQBDLT͸ύϥϝʔλʔͷܕΛอͬͨ··ͳͷͰؔ਺ͷ໭Γ஋
    ͳͲʹύϥϝʔλʔͷܕΛ൓өͨ͠ܕΛ໭Γ஋ʹ൓өͰ͖Δ
    w ܕύϥϝʔλʔͷύοέʔδରԠ͸4XJGU6*ʹ࢖ΘΕͦ͏ͳงғؾΛ͍ͩͯ͠
    Δ͕ͦΜͳ͜ͱ͸ͳ͔ͬͨ
    ܕύϥϝʔλʔͷύοέʔδରԠᶇ

    View full-size slide

  25. ৽ػೳᶅ
    0XOFSTIJQ

    View full-size slide

  26. 0XOFSTIJQ
    ߏ଄ମͰ΋Ϋϥεͷ୅ΘΓΛ͍ͨ͠ɻ
    DMBTTͷར఺͸ΦϒδΣΫτͷղ์λΠϛϯάͰEFJOJU
    ͕ݺͼग़͞Ε
    ΔɻEFJOJU
    ͷಛੑΛར༻ͯ͠ΦϒδΣΫτϋϯυϧ IBOEMF
    Λղ์͢
    Δ࢓૊ΈΛ࡞͍ͬͯΔɻ
    TUSVDUͰ΋޻෉ ࡉ޻
    ͢Ε͹DMBTTΛ࢖Θͳͯ͘΋
    ࣮ݱͰ͖Δ͔΋ʜ

    View full-size slide

  27. 0XOFSTIJQ
    ߏ଄ମͰ΋Ϋϥεͷ୅ΘΓΛ͍ͨ͠ɻ
    struct FileDescriptor: ~Copyable {


    private var fd: CInt




    init(descriptor: CInt) { self.fd = descriptor }


    func write(buffer: [UInt8]) throws {


    let written = buffer.withUnsafeBufferPointer {


    Darwin.write(fd, $0.baseAddress, $0.count)


    }


    // ...


    }




    consuming func close() {


    Darwin.close(fd)


    }




    deinit {


    Darwin.close(fd)


    }


    }


    View full-size slide

  28. 0XOFSTIJQ
    ߏ଄ମͰ΋Ϋϥεͷ୅ΘΓΛ͍ͨ͠ɻ
    struct FileDescriptor: ~Copyable {


    private var fd: CInt




    init(descriptor: CInt) { self.fd = descriptor }


    func write(buffer: [UInt8]) throws {


    let written = buffer.withUnsafeBufferPointer {


    Darwin.write(fd, $0.baseAddress, $0.count)


    }


    // ...


    }




    consuming func close() {


    Darwin.close(fd)


    }




    deinit {


    Darwin.close(fd)


    }


    }


    ίϐʔͰ͖ΔΛ൱ఆ͢Δϓϩτίϧ໊͕ڧҾ
    DPOTVNJOHΛ͚ͭΔͱଞͷؔ਺ΑΓ΋࠷ޙʹݺͼग़
    ͞ΕΔ͔ΛίϯύΠϧϕʔεͰνΣοΫ

    View full-size slide

  29. ৽ػೳ
    4XJGUNBDSPT

    View full-size slide

  30. 4XJGUNBDSPTᶃ
    4XJGU"UUSJCVUFTͰ"QQMF͕΍͖ͬͯͨ͜ͱΛ9DPEFͷϓϥάΠϯԽ
    w 1BDLBHFͰఏڙ͞ΕΔϓϥάΠϯ MPDBM͔ΒऔΓࠐΈɺDPMMFDUJPO͔Βऔಘ

    w Ͱ࢝·Δ'SFFTUBOEJOHNBDSPT
    w !Ͱ࢝·ΔBUUBDIFENBDSPT
    w !Ͱ࢝·ΔͷͰ4XJGU"UUSJCVUFTΛ4XJGUNBDSPTͱݟ෼͚͕͔ͭͳ͍
    w NBDSPΛॻ͘ͱ4XJGUݴޠͷղੳ໦Λ౉͞ΕΔͷͰɺผͷιʔείʔυ จࣈ

    ΁ͱม׵͢Δ࢓༷

    View full-size slide

  31. 4XJGUNBDSPTᶄ
    4XJGUNBDSPT޲͚ͷ4XJGU"UUSJCVUFT
    w એݴଐੑ %FDMBSBUJPO"UUSJCVUFT
    ͷҰ෦
    w BUUBDIFE
    w 1FFSNBDSPTɺ.FNCFSNBDSPɺ.FNCFSBUUSJCVUFɺ"DDFTTPS
    NBDSPTɺ$POGPSNBODFNBDSPTɺ
    w GSFFTUBOEJOH
    !Ͱ࢝·Δ4XJGU.BDSPΛ࡞Ζ͏ͱ͢Δͱ!Ͱ࢝·
    Δ4XJGU"UUSJCVUFTΛ࢖͏͜ͱʹͳΔ

    View full-size slide

  32. 4XJGUNBDSPTᶅ
    4XJGUNBDSPTͷ༻్
    w ϑϨʔϜϫʔΫಠࣗͷػೳΛ૊ΈࠐΉࡍͷఆܕจ ϘΠϥϓϨʔτίʔυ
    هड़Λ
    4XJGUNBDSPTͰஔ͖׵͑Δ͜ͱ͕Մೳ
    w "QQMFۘ੡ͷϑϨʔϜϫʔΫ΋࠾༻
    w 4XJGU%BUB
    w 0CTFSWBUJPOGSBNFXPSL
    w ιϑτ΢ΣΞΞʔΩςΫνϟ΁ద߹ͤ͞ΔͨΊͷϚΫϩ
    w ରԠͦ͠͏ͳΞʔΩςΫνϟ 7*1&3ɺ3FEVYͱ͔

    View full-size slide

  33. 4XJGUNBDSPTᶆ
    4XJGUNBDSPTΛࢼ͢
    w 9DPEF/FX1BDLBHF͔Β4XJGUNBDSPTΛબ୒Մೳ
    w σϑΥϧτͰTUSJOHJGZͷαϯϓϧؚ͕·ΕΔ
    w 9DPEFCFUBͰ͸MPDBM͔ΒऔΓࠐΈͰ͖ͳ͔ͬͨ

    View full-size slide

  34. ͔͜͜Β͸4XJGUNBDSPTʹ
    ࢥ͏ͱ͜Ζʜ

    View full-size slide

  35. 4XJGU४ඪ४ػೳΛ૊ΈࠐΉͨΊʹ4XJGU"UUSJCVUFTʹ৽ఆٛΛ௥Ճ͢Δ
    ͷ΋ݶ౓͕͋Δɻ
    ˡϑϨʔϜϫʔΫʹґଘ͢Δఆ͕ٛཉ͍͔͠ΒϚΫϩػೳΛఏڙ͠Α͏ɻ
    Έ͍ͨͳϊϦͳͷͰ͸
    4XJGUNBDSPTొ৔ͷഎܠΛߟ͑Δ
    IUUQTTQFBLFSEFDLDPNOPUPSPJEBQQMFHBZBSJUBJGBOHUJTXJGUBUUSJCVUFT
    ࢀߟ "QQMF͕
    ΍Γ͍ͨ์୊4XJGU"UUSJCVUFT4QFBLFS%FDL

    View full-size slide

  36. 4XJGUNBDSPT͸ڧྗ͚ͩͲɺϑϨʔϜϫʔΫΛఏڙ
    ͢Δͱ͔ɺϚΫϩ༻1BDLBHFΛҡ࣋Ͱ͖Δ ମྗ͕͋
    Δ
    ૊৫ ίϛϡχςΟɺاۀ
    ʹݶఆ͞ΕΔͷͰ͸
    ϞόΠϧϓϥοτϑΥʔϜʹ͍ͭͯ͸͔ͭͯ΄Ͳͷ
    ։ൃऀ͕૿͑ΔΘ͚Ͱ͸ͳ͍ͷʹʜ
    4XJGUNBDSPT΁ͷٙ໰

    View full-size slide

  37. 4XJGUNBDSPͷαϯϓϧ

    View full-size slide

  38. 0CTFSWBUJPOͷαϯϓϧίʔυ
    4XJGUNBDSPΛ࢖༻
    IUUQTHJUIVCDPNOPUPSPJE(BNF1MBZFS4UBUVT

    View full-size slide

  39. 0CTFSWBUJPOͷαϯϓϧ
    wIUUQTHJUIVCDPNOPUPSPJE
    (BNF1MBZFS4UBUVT
    wήʔϜϓϨΠϠʔͷঢ়ଶΛදࣔ
    มߋͰ͖Δ
    w1FSTPOΛ͜Ε·Ͱͷ࣮૷ɺ
    0CTFSWBUJPOΛ࢖ͬͨ΋ͷΛࢼ͢
    ͜ͱ͕Ͱ͖Δ
    w9DPEFͷΤσΟλͰϚΫϩల։͕
    Մೳ
    (BNF1MBZFS4UBUVT

    View full-size slide

  40. 0CTFSWBUJPOGSBNFXPSLͷϚΫϩͦͷ
    0CTFSWBUJPOGSBNFXPSLͷϚΫϩͦͷ
    0CTFSWBUJPOGSBNFXPSLͷଐੑ؂ࢹΦϒδΣΫτ

    View full-size slide

  41. ͜Ε·Ͱͷ4XJGU6*ͷ൥ࡶ͞͸ͳΜͩͬͨͷ͔
    0CTFSWBCMF0CKFDUɺ!0CTFSWFE0CKFDUɺ
    !4UBUF0CKFDUɺ!&OWJSPONFOU0CKFDU

    0CTFSWBUJPOGSBNFXPSL

    View full-size slide

  42. ·ͱΊ
    w4XJGU͸ੑೳ޲্ɺ$ͷޓ׵ੑ͕Ξϐʔϧ͞ΕΔ΋࠷৽ػೳ͸
    গͳΊͰ͢
    w4XJGU"UUSJCVUFTͷػೳΛϓϥάΠϯԽͨ͠Α͏ͳ4XJGUNBDSPT͕ొ
    ৔͠·ͨ͠ɻ
    w"QQMF͸4XJGUNBDSPTΛఏڙ࡞ΔͨΊʹ4XJGU"UUSJCVUFT΁৽͘͠ఆ
    ٛΛ௥Ճ͠·ͨ͠

    View full-size slide

  43. ࢀߟ
    w 8IBU`TOFXJO4XJGU88%$7JEFPT
    "QQMF%FWFMPQFS
    w IUUQTEFWFMPQFSBQQMFDPNXXED
    w &YQBOEPO4XJGUNBDSPT88%$7JEFPT
    "QQMF%FWFMPQFS
    w IUUQTEFWFMPQFSBQQMFDPNXXED
    w 8SJUF4XJGUNBDSPT88%$7JEFPT"QQMF
    %FWFMPQFS
    w IUUQTEFWFMPQFSBQQMFDPNXXED
    w .FFU4XJGU%BUB88%$7JEFPT"QQMF
    %FWFMPQFS
    w IUUQTEFWFMPQFSBQQMFDPNXXED
    w 8IBUTOFXJO4XJGUͷཁ໿
    TXJGU@@@TVNNBSZ4QFBLFS%FDL
    w IUUQTTQFBLFSEFDLDPNVIPPJTXJGU
    TVNNBSZ
    w 4XJGUͰಋೖ͞ΕΔ"UUBDIFE.BDSPʹ͍ͭͯ
    2JJUB
    w IUUQTRJJUBDPNGSPTUFE@CJSEJUFNT
    GCGD
    w 0CTFSWBUJPOc"QQMF%FWFMPQFS%PDVNFOUBUJPO
    w IUUQTEFWFMPQFSBQQMFDPN
    EPDVNFOUBUJPOPCTFSWBUJPO

    View full-size slide