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

シン・スウィフト 5.7 ~ Swift5.7を振り返る ~ / New Swift 5.7

シン・スウィフト 5.7 ~ Swift5.7を振り返る ~ / New Swift 5.7

「シン・スウィフト 5.7 ~ Swift5.7を振り返る ~」登壇:freddi 監修:shinzan

※こちらは以下イベントの資料です
https://yj-meetup.connpass.com/event/247614/

A3966f193f4bef226a0d3e3c1f728d7f?s=128

LINE Developers
PRO

June 07, 2022
Tweet

More Decks by LINE Developers

Other Decks in Technology

Transcript

  1. γ ϯΦ ϒδ ΣΫ ς Ο ϒ $

  2. γ ϯ ɾ ε ΢ Ο ϑ τ S W

    I F T: 5.7 ొஃGSFEEJ؂मTIJ[ ʙ۩ମྫͰֶͿɺ4XJGU͔Βͷzܕz͍ͭͯʙ 88%$&YUFOEFE5PLZP +VOFUI 
  3. ొஃऀͱ؂मऀ ؂मTIJ[ ొஃGSFEEJ -*/&'VLVPLBגࣜձࣾॴଐ -*/&גࣜձࣾॴଐ

  4. BOZΩʔϫʔυͷಋೖ

  5. BOZΩʔϫʔυͷಋೖ protocol P {} let value: P any 4&

  6. lϓϩτίϧΛܕએݴʹར༻͢Δz w ϓϩτίϧΛܕʢએݴʣͱͯ͠࢖͏ͱଘࡏܕʹͳΔ w lଘࡏܕz͸ɺ&YJTUFOUJBM$POUBJOFSͱ͍͏࢓૊Έ͕ ҉໧తʹಇ͘ protocol P {} let

    value: P ͜ͷ1͸ଘࡏܕ 4&
  7. &YJTUFOUJBM$POUBJOFS w ʮಉ͡ϓϩτίϧಉ࢜ʯͱ͍͏ಛੑΛ࣮ݱ͢ΔzೖΕ෺z w ࣮ߦ࣌ʹಇ͘ͷͰɺ͍··ͰͷίʔυͰͷҙࣝ͸ബ͍ struct A: P {}; struct

    B: P {} var value: P = A() value = B() ͜ͷ1͸ଘࡏܕ #͸1ʹ४ڌͯ͠ΔͷͰ0, WBMVF͸ଘࡏܕ1Ͱ͋Γɺ1ͷܕΛड͚ೖΕΔίϯςφ 4&
  8. ଘࡏܕͱ&YJTUFOUJBM$POUBJOFSͷσϝϦοτ w zೖΕ෺z͸ΦϦδφϧͷܕΑΓ΋αΠζ͕େ͖͍৔߹΋ w ద੾ʹೖΕ෺ͷத਎ʢม਺΍ؔ਺ʣΛݺͼग़ͨ͢Ίͷ ࢓૊Έ͕͋ΔͷͰɺͦͷ෼Φʔόʔϔου͕͋Δ 1 "1 #1 $1

    %1˞ ˞ίϯςφΑΓ΋αΠζ͕େ͖͍৔߹ɺͦͷ෼ͷՙ୆Λ૿΍ͯͭ͠ͳ͛ΔΠϝʔδ 4&
  9. Ͱ͸ʁ w ٯʹany͕ͳ͍ੈքΛߟ͑ͯΈΔ w ҎԼͷ1ͱ15ZQFͲͪΒ͕௨ৗͷܕએݴͳͷͩΖ͏͔ let value: P let value:

    PType 4&
  10. ݟͨ໨͚ͩͰ͸ʮͲΕ͕ଘࡏܕ͔ʯΘ͔Βͳ͍ w 1͸ϓϩτίϧ15ZQFͷ࣮૷͔΋͠Εͳ͍ w 15ZQF͸ϓϩτίϧ1ͷ࣮૷͔΋͠Εͳ͍ w ࣮૷Λݟʹߦ͚͹Θ͔Δ͕ɺखݩʹͳ͍৔߹͸ʁ 4&

  11. ͳͥ1ͱ15ZQF͕໰୊ʹͳΔͷ͔ w ύϑΥʔϚϯεΛٻΊΔͱ͖Λߟ͑Δ w ଘࡏܕʹΑΔύϑΥʔϚϯεͷ௿Լ͸ආ͚͍ͨͷͳΒʁ w ଘࡏܕΛආ͚ͯɺ۩ମతͳܕͰએݴ͍ͨ͠ w ͔͠͠ʜ w

    1ͱ15ZQF͸ݟͨ໨্۠ผ͕͔ͭͳ͍ͷ͕໰୊ 4&
  12. Ͱ͸any͸Ͳ͏ॿ͚ͯ͘ΕΔͷ͔ GSPN4XJGU w ଘࡏܕʹ͸BOZΛ͚ͭͳ͚Ε͹ͳΒͳ͍ w ଘࡏܕ͕͙͢ʹݟ෼͚ΒΕΔˠΦʔόʔϔουΛ๷͙ ⭕ let value: any

    P ❌ let value: any PType 4& 5 6
  13. BOZͱBTTPDJBUFEUZQF

  14. BTTPDJBUFEUZQFͱܕফڈ w BTTPDJBUFEUZQFͷ͋ΔϓϩτίϧΛଘࡏܕͱͯ͠ར༻ w 4XJGU·Ͱ͸ෆՄೳˠͰ͸ՄೳʹͳΔ protocol P { associatedtype T

    } let value: any P 4&
  15. BTTPDJBUFEUZQFͱܕফڈ w Ͱ͸ɺ͍··Ͱ͸Ͳ͏͢Δ΂͖͔ͩͬͨʁ w ࠓ·Ͱܕফڈͱݺ͹ΕΔٕज़͕ඞཁʹ struct AnyP<T>: P where T:

    P { var p: T init(_ p: T) { self.p = p } } 4&
  16. ܕফڈΛࣗલͰߦ͏σϝϦοτ w ϘΠϥʔϓϨʔτίʔυͷ૿࢈ʹͭͳ͕ͬͯ͠·͏ w ফڈ͍ͨ͠ܕ͝ͱʹlܕফڈ༻ͷܕzΛ࡞Δ w Լखʹ࣮૷͢ΔͱΦʔόʔϔου͕૿͑Δ 4&

  17. 4XJGU͔ΒͷBOZ w BOZ BTTPDJBUFEUZQF͕ίϯύΠϧͰ͖ΔΑ͏ʹͳΔ protocol P { associatedtype T }

    let value: any P ⭕ 4&
  18. 4XJGU͔ΒͷBOZ w ྫ͑͹ҎԼͷΑ͏ͳ"SSBZ΋ॻ͚Δ class A: P { typealias T =

    Int } class B: P { typealias T = Double } let array: [any P] = [A(), B()] 4&
  19. BOZͷخ͠͞ w ଘࡏܕͱͦͷଞΛݟ෼͚Δࣄ͕Ͱ͖ΔΑ͏ʹ w ίʔυ͔ΒΑΓ໌֬ʹύϑΥʔϚϯεΛҙࣝͰ͖Δ w BTTPDJBUFEUZQF͋Γͷଘࡏܕ͕એݴՄೳʹ w σϝϦοτ΋͋Δࣗલͷܕফڈ͕ෆཁʹͳΔ 4&

  20. TPNFͷଟػೳԽ

  21. TPNFͱ͸ w TPNF1ͷΑ͏ʹTPNFʴϓϩτίϧͱએݴ͞Εͨܕ͸ 0QBRVF5ZQF ଘࡏܕͱ͸ผ ͱݺ͹ΕΔ some P

  22. 0QBRVF5ZQFͷϝϦοτ w &YJTUFOUJBM$POUBJOFSΛར༻͠ͳ͍ w Φʔόʔϔου͕গͳ͍ͱ͍͏ϝϦοτ w ฦ٫͢ΔܕΛ࣮૷ऀ͕ҙࣝ͠ͳ͍Α͏ʹͰ͖Δ w ۩ମతͳܕΛ֎ʹग़͢͜ͱ͕ཁٻ͞Εͳ͍ w

    ͔ͭଘࡏܕͷΑ͏ͳΦʔόʔϔουΛ๷͙ w ྫͱͯ͠4XJGU6*ͷTPNF7JFX
  23. ͍··ͰͷTPNF w 4XJGU6*ͷTPNF7JFXͷΑ͏ʹBTTPDJBUFEUZQFͷ͋Δ ܕΛฦ٫Ͱ͖ͨ protocol P { associatedtype T }

    func someFunc() -> some P { PType() // PType conforms P }
  24. ͜Ε͔ΒͷTPNFͦͷ̍ʢ4XJGUdʣ w ߏ଄తͳܕͷฦ٫ʹ΋ར༻͕ՄೳʹͳΔ w ΑΓந৅౓ͷߴ͍࣮૷Λॻ͘͜ͱ͕Ͱ͖Δ func someFunc() -> [some P]

    { [PType()] } func someFunc() -> [some Hashable: some P] { [1: PType(), 2: PType()] } 4&
  25. ͜Ε͔ΒͷTPNFͦͷ̎ʢ4XJGUdʣ w ҎԼͷΑ͏ͳδΣωϦΫεͷ͋Δؔ਺Λߟ͑Δ w ࣮͸ແବ͕ଟ͍ͷͰ͸ͳ͍ͷͩΖ͏͔ func someFunc<A: P, B: P>(arg1:

    A, arg2: B) { // do smith } 4&
  26. ͜Ε͔ΒͷTPNFͦͷ̎ʢ4XJGUdʣ w XIFSFʹΑΔBTTDPDJBUFEUZQFͷ੍໿͕ඞཁͳ͍ w TPNFʹΑΔ੍໿Ͱ΋े෼ͳͷͰ͸ͳ͍͔ w δΣωϦΫεͷಛघԽʹΑΔσϝϦοτ someFunc(arg1: A, arg2:

    B) someFunc(arg1: B, arg2: B) someFunc(arg1: B, arg2: A) ͜ͷΑ͏ͳίʔϧΛͨ͠৔߹ ͭ෼ͷTPNF'VODͷ࣮૷͕ίϯύΠϧ࣌ʹੜ੒ ಛघԽ  ΞϓϦͷαΠζ͕ͪΐͬͱ૿͑Δ͔΋ 4&
  27. ͜Ε͔ΒͷTPNFͦͷ̎ʢ4XJGUdʣ w δΣωϦΫεʹΑΔந৅ΛTPNFͰ୅༻͕Մೳ w ಛघԽ͕ແ͘ɺίϯύΠϧ࣌ʹੜ੒͞ΕΔίʔυ͕ݮগ func someFunc(arg1: some A, arg2:

    some B) { // do smth } 4&
  28. ϓϩτίϧͷlܕύϥϝʔλz

  29. BTTPDJBUFUZQFͷlύϥϝʔλԽz w BTTPDJBUFUZQF ܕύϥϝʔλͷه๏͕௥Ճ protocol T<P, Q> { associatedtype P

    associatedtype Q } 4&
  30. BTTPDJBUFUZQFͷlύϥϝʔλԽz w l੍໿zͱͯ͠ͷར༻͕Մೳʹ w 5ͷBTTPDJBUFEUZQF΁ͷ੍໿͸ԼͷΑ͏ʹॻ͚Δ // ैདྷͷॻ͖ํ func call1<G: T>(a:

    G) where G.P == Int, G.Q == Double // ͜Ε͔Βͷॻ͖ํ func call2(a: some T<Int, Double>) 4&
  31. มߋʹ൐͏ඪ४ϥΠϒϥϦͷมߋ 6OEFS3FWJFX w $PMMFDUJPOͳͲͷϓϩτίϧʹύϥϝʔλΛ௥Ճ ԼهͷΑ͏ͳσϑΥϧτҾ਺͕Մೳ func call<C: Collection>(collection: C =

    [1, 2, 3]) 4&
  32. TPNF ϓϩτίϧͷύϥϝʔλΛ ར༻ͯ͠ΈΔ

  33. "1*3FRVFTUϓϩτίϧ w ҎԼͷΑ͏ͳϓϩτίϧΛར༻ͨ͠"1*ίʔϧΛߟ͍͑ͨ protocol APIRequest { associatedtype Response: Decodable var

    path: String { get } }
  34. "1*3FRVFTU΁ͷ४ڌ w ϢʔβʔͷϦετΛऔΔ"1*ϦΫΤετ struct User: Decodable {} struct GetUserListRequest: APIRequest

    { typealias Response = [User] var path: String { "/list "} }
  35. "1*3FRVFTUΛར༻ͯ͠3FTU"1*Λίʔϧ͢Δؔ਺ w ैདྷͷํ๏Ͱ΍ͬͯΈΔͱҎԼͷΑ͏ʹͳΔ func get<Request: APIRequest>(_ request: Request) async throws

    -> Request.Response
  36. "1*3FRVFTUϓϩτίϧͷܕύϥϝʔλ w 4XJGU͔ΒೖΔܕύϥϝʔλΛ࢖ͬͯΈΔ protocol APIRequest <Response> { associatedtype Response: Decodable

    var path: String { get } }
  37. "1*3FRVFTUͷܕύϥϝʔλΛར༻ͨؔ͠਺ w ҎԼͷΑ͏ʹͳΔʢ࣮૷͸΄΅ಉ͡Ͱྑ͍ʣ w ͋·ΓมΘͬͯͳ͍Α͏ʹ΋ݟ͑Δ͕ʜ func get<Request: APIRequest>(_ request: Request)

    async throws -> Request.Response ↓ func get<Response>(_ request: some APIRequest<Response>) async throws -> Response
  38. ܕύϥϝʔλΛར༻ͨ͠ʢࠓճͷʣϝϦοτ w ࣮͸ίϯςΩετ͸ൺֱతεοΩϦ͍ͯ͠Δ w 3FTQPOTFδΣωϦΫεͷ੍໿͕ݮগ w 3FTQPOTFͱ"1*3FRVFTUͷؔ܎ੑ͕͸͖ͬΓ func get<Response>(_ request:

    some APIRequest<Response>) async throws -> Response
  39. ·ͱΊ

  40. ·ͱΊ w BOZͱTPNFपΓͷ4XJGUͷܕͷมߋ w BOZͱ&YJTUFOUJBM$POUBJOFS w QSPUPDPMͷܕύϥϝʔλͱͦͷ۩ମྫ w ঺հͰ͖ͳ͔ͬͨ΋ͷͰॏཁͳ΋ͷ΋͋Δ w

    Aif let hogeAपΓͷมߋ 4&  w ৽͍࣌ؒ͠ͷ֓೦ͷಋೖ 4& 
  41. ܶऴ 88%$͝ظ଴͍ͩ͘͞