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

    λεΫͷ༏ઌ౓্֨͛ػೳ w λεΫ಺ͷ؀ڥม਺ڞ༗ػೳ w ฒߦੑ࣌ͷಈ࡞τϨʔε
  2. 8IBU`TOFXJO4XJGU88%$ IUUQTEFWFMPQFSBQQMFDPNXXED ϓϩδΣΫτશମͷऔΓ૊Έ ৽ػೳᶃJGFMTF TXJUDIΛࣜͱͯ͠࢖༻Մೳʹ ৽ػೳᶄܕύϥϝʔλʔͷύοέʔδରԠ 4XJGUͷੑೳ໘޲্ͷ࿩ ৽ػೳ 4XJGU"UUSJCVUFT͕ఏڙ͍ͯͨ͠ػೳΛϓ ϥάΠϯͱͯ͠ར༻Մೳʹ

    ੑೳ໘޲্ͨ͠4XJGUΛ࢖ͬͯϑϨʔϜϫʔΫΛ࡮৽த ৽ػೳᶅDMBTTͰͰ͖Δ͜ͱΛTUSVDUʹ೚͍ͤͨ ৽ػೳᶇฒߦੑͷॊೈੑΛڧԽ ˢͷ࠾༻ྫ ৽ػೳᶆ$ ޲͚ͷίʔυδΣωϨʔλʔ
  3. 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ଐੑΛ௥Ճ͢Δɻ
  4. 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ଐੑΛ௥Ճ͢Δɻ
  5. 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΋লུͰ͖Δɻ
  6. ܕύϥϝʔλʔͷύοέʔδରԠᶃ func query<Payload>( _ item: Request<Payload> ) -> Payload func

    query<Payload1, Payload2>( _ item1: Request<Payload1>, _ item2: Request<Payload2> ) -> (Payload1, Payload2) func query<Payload1, Payload2, Payload3>( _ item1: Request<Payload1>, _ item2: Request<Payload2>, _ item3: Request<Payload3> ) -> (Payload1, Payload2, Payload3) func query<Payload1, Payload2, Payload3, Payload4>( _ item1: Request<Payload1>, _ item2: Request<Payload2>, _ item3: Request<Payload3>, _ item4: Request<Payload4> ) -> (Payload1, Payload2, Payload3, Payload4)
  7. func query<Payload>( _ item: Request<Payload> ) -> Payload func query<Payload1,

    Payload2>( _ item1: Request<Payload1>, _ item2: Request<Payload2> ) -> (Payload1, Payload2) func query<Payload1, Payload2, Payload3>( _ item1: Request<Payload1>, _ item2: Request<Payload2>, _ item3: Request<Payload3> ) -> (Payload1, Payload2, Payload3) func query<Payload1, Payload2, Payload3, Payload4>( _ item1: Request<Payload1>, _ item2: Request<Payload2>, _ item3: Request<Payload3>, _ item4: Request<Payload4> ) -> (Payload1, Payload2, Payload3, Payload4) func query<each Payload>(_ item: repeat Request<each Payload>) -> (repeat each Payload) ύϥϝʔλ਺Λ૿΍ͨ͠ಉؔ͡਺ఆ͕ٛඞཁͩͬͨ ΋ͷ͕ͭͷύϥϝʔλʔͱͯ͠ѻ͏͜ͱ͕Մೳʹ ܕύϥϝʔλʔͷύοέʔδରԠᶄ
  8. ܕύϥϝʔλʔͷύοέʔδରԠᶆ 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Λ ૊Έ߹ΘͤΔͱม׵Ͱ͖ΔܕͷόϦΤʔγϣϯ͸૿͑Δ
  9. 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) } }
  10. 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Λ͚ͭΔͱଞͷؔ਺ΑΓ΋࠷ޙʹݺͼग़ ͞ΕΔ͔ΛίϯύΠϧϕʔεͰνΣοΫ
  11. 4XJGUNBDSPTᶃ 4XJGU"UUSJCVUFTͰ"QQMF͕΍͖ͬͯͨ͜ͱΛ9DPEFͷϓϥάΠϯԽ w 1BDLBHFͰఏڙ͞ΕΔϓϥάΠϯ MPDBM͔ΒऔΓࠐΈɺDPMMFDUJPO͔Βऔಘ  w Ͱ࢝·Δ'SFFTUBOEJOHNBDSPT w !Ͱ࢝·ΔBUUBDIFENBDSPT

    w !Ͱ࢝·ΔͷͰ4XJGU"UUSJCVUFTΛ4XJGUNBDSPTͱݟ෼͚͕͔ͭͳ͍ w NBDSPΛॻ͘ͱ4XJGUݴޠͷղੳ໦Λ౉͞ΕΔͷͰɺผͷιʔείʔυ จࣈ ྻ ΁ͱม׵͢Δ࢓༷
  12. 4XJGUNBDSPTᶄ 4XJGUNBDSPT޲͚ͷ4XJGU"UUSJCVUFT w એݴଐੑ %FDMBSBUJPO"UUSJCVUFT ͷҰ෦ w BUUBDIFE w 1FFSNBDSPTɺ.FNCFSNBDSPɺ.FNCFSBUUSJCVUFɺ"DDFTTPS

    NBDSPTɺ$POGPSNBODFNBDSPTɺ w GSFFTUBOEJOH !Ͱ࢝·Δ4XJGU.BDSPΛ࡞Ζ͏ͱ͢Δͱ!Ͱ࢝· Δ4XJGU"UUSJCVUFTΛ࢖͏͜ͱʹͳΔ
  13. 4XJGUNBDSPTᶅ 4XJGUNBDSPTͷ༻్ w ϑϨʔϜϫʔΫಠࣗͷػೳΛ૊ΈࠐΉࡍͷఆܕจ ϘΠϥϓϨʔτίʔυ هड़Λ 4XJGUNBDSPTͰஔ͖׵͑Δ͜ͱ͕Մೳ w "QQMFۘ੡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