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

存在型に `any` を付けていないときにコンパイルエラーにする / How to produ...

存在型に `any` を付けていないときにコンパイルエラーにする / How to produce a compile error when writing an existential type without the `any` keyword

存在型に `any` を付けていないときにコンパイルエラーにする / How to produce a compile error when writing an existential type without the `any` keyword

potatotips #81 iOS/Android開発Tips共有会
2023/02/21 19:00〜
https://potatotips.connpass.com/event/272353/

登壇ノートはこちら: https://zenn.dev/treastrain/articles/555d4a2fc1b40b

treastrain / Tanaka Ryoga

February 21, 2023
Tweet

More Decks by treastrain / Tanaka Ryoga

Other Decks in Programming

Transcript

  1. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. USFBTUSBJO5BOBLB3ZPHB

    1 potatotips #81 
 iOS / Android ։ൃ Tips ڞ༗ձ #potatotips ଘࡏܕʹʮBOZʯΛ෇͚͍ͯͳ͍ͱ͖ʹ 
 ίϯύΠϧΤϥʔʹ͢Δ 4&1JFDFNFBMBEPQUJPOPGVQDPNJOHMBOHVBHF JNQSPWFNFOUTͷ࢖༻ྫ
  2. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. "CPVUNF

    USFBTUSBJO5BOBLB3ZPHB 2 4XJGU J04 XBUDI04 $PSF/'$ 044 
 ɹɹJU`TNZNPUIFSUPOHVF 
 ɹɹ࠷ۙ.JTTLFZJPΛ͸͡Ί·ͨ͠ 
 ɹɹɹɹ.BTUPEPOͳͲͱ΋ܨ͕ΕΔ'FEJWFSTFͰ͢ ɹɹɹɹɹɹɹɹɹɹ!USFBTUSBJOɹɹIUUQTUSFUKQ 
 
 %F/"$P -UE 
 ɹɹJ04"QQ%FWFMPQFSʢ"QSJM$VSSFOUʣ
  3. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ௚લͰൃද಺༰Λมߋ͠·ͨ͠

    ʮΫϩʔδϟΛ༻͍ͨඇಉظύλʔϯΛ"TZOD4USFBNʹม͑ͯΈͨʯͷ༧ఆͰͨ͠ 3 QPUBUPUJQTuQPUBUPUJQTQPUBUPUJQT8JLJIUUQTHJUIVCDPNQPUBUPUJQTQPUBUPUJQTXJLJQPUBUPUJQT
  4. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ௚લͰൃද಺༰Λมߋ͠·ͨ͠

    ʮΫϩʔδϟΛ༻͍ͨඇಉظύλʔϯΛ"TZOD4USFBNʹม͑ͯΈͨʯͷ༧ఆͰͨ͠ 4 9DPEFCFUB &G ࠷৽χϡʔε"QQMF%FWFMPQFSIUUQTEFWFMPQFSBQQMFDPNKQOFXTSFMFBTFT JEG
  5. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ଘࡏܕ

    &YJTUFOUJBM5ZQFT 5 protocol Fruit {} struct Apple: Fruit {} struct Banana: Fruit {} func fruit1() -> Fruit { Apple() } var fruit2: Fruit = fruit1() fruit2 = Banana() // ✅
  6. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 0QBRVFܕ

    0QBRVF5ZQFTʢ4XJGU ɺ4XJGU ʣ 6 protocol Fruit {} struct Apple: Fruit {} struct Banana: Fruit {} // Swift 5.1+ func fruit3() -> some Fruit { Apple() } // Swift 5.7+ var fruit4: some Fruit = fruit3() fruit4 = Banana() // ❌ Cannot assign value of type 'Banana' to type 'some Fruit'
  7. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ଘࡏܕ

    &YJTUFOUJBM5ZQFTʢ4XJGU ʣ 7 protocol Fruit {} struct Apple: Fruit {} struct Banana: Fruit {} // Swift 5.6+ func fruit5() -> any Fruit { Apple() } // Swift 5.6+ var fruit6: any Fruit = fruit5() fruit6 = Banana() // ✅
  8. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ଘࡏܕ

    &YJTUFOUJBM5ZQFTʢ4XJGUΑΓ΋લ·Ͱʣ 8 protocol Fruit {} func fruit1() -> Fruit { /* ... */ } var fruit2: Fruit = fruit1() protocol Fruit {} func fruit5() -> any Fruit { /* ... */ } var fruit6: any Fruit = fruit5()
  9. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ଘࡏܕ

    &YJTUFOUJBM5ZQFTʢ4XJGUҎ߱Ͱ͸ʣ 9 protocol Fruit {} func fruit1() -> Fruit { /* ... */ } var fruit2: Fruit = fruit1() protocol Fruit {} func fruit5() -> any Fruit { /* ... */ } var fruit6: any Fruit = fruit5()
  10. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ʮ4XJGUҎ߱Ͱ͸ʜʜʯ

    4XJGUͰ͸ഁյతมߋ͕ଟ͘ݟࠐ·ΕΔ w 4XJGUܥͷϓϩδΣΫτ͔Β4XJGUܥʹҠߦ͢Δʹ͸ରԠ͕ඞཁ w Ͱ͖Ε͹ࠓͷ͏͔ͪΒ4XJGU޲͚ͷରԠΛ͓͖͍ͯͨ͠ w ʮBOZΛ෇͚͍ͯͳ͍ଘࡏܕʯͷ͢΂ͯΛͲ͏΍ͬͯ୳͢ʜʜʁ w ʮ4XJGUҎ߱Ͱ͸ʜʜʯͳͲͷػೳͷҰ෦Λɺ 
 ໌ࣔతʹ༗ޮʹ͓͖͍ͯͨ͠ w ίϯύΠϧΤϥʔɾܯࠂͳͲͰ༰қʹؾ͚ͮΔΑ͏ʹͳΔ 10
  11. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 11

    1JFDFNFBMBEPQUJPOPGVQDPNJOH MBOHVBHFJNQSPWFNFOUT 4&ɹকདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ ೔ຊޠ༁͸IUUQTHJUIVCDPNTU[O4XJGU1PEDBTUCMPCNBJOESBGUT4XJGUকདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻NEΑΓ
  12. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻

    4XJGU w 4XJGUίϯύΠϥʹ௥Ճ͞Εͨ 
 -enable-upcoming-feature XϑϥάΛ༻͍Δ w Xʹ͸ద༻͍ͤͨ͞কདྷ௥Ճ༧ఆͷݴޠվળͷػೳͷ໊લΛࢦఆ͢Δ w &YJTUFOUJBM"OZ w 4USJDU$PODVSSFODZ w #BSF4MBTI3FHFY-JUFSBMTɹFUD 12 IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPOCMPCNBJOQSPQPTBMTQJFDFNFBMGVUVSFGFBUVSFTNE
  13. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. -enable-upcoming-feature

    ExistentialAny 4XJGU ͔ͭ4XJGUΑΓ΋લ·Ͱ 13 protocol Fruit {} func fruit1() -> Fruit { /* ... */ } var fruit2: Fruit = fruit1() protocol Fruit {} func fruit5() -> any Fruit { /* ... */ } var fruit6: any Fruit = fruit5() 6TFPGQSPUPDPM'SVJUBTBUZQF NVTUCFXSJUUFOBOZ'SVJU
  14. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻

    ࢖༻ྫ w 4XJGU1BDLBHFͷλʔήοτʹద༻͢Δ w 9DPEFϓϩδΣΫτͷλʔήοτʹద༻͢Δ 14
  15. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 4XJGU1BDLBHFͷλʔήοτʹద༻͢Δ

    কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ 15 // PackageDescription 5.8+ public struct SwiftSetting: Encodable { // ... @available(_PackageDescription, introduced: 5.8) public static func enableUpcomingFeature( _ name: String, _ condition: BuildSettingCondition? = nil ) -> SwiftSetting // ... }
  16. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 4XJGU1BDLBHFͷλʔήοτʹద༻͢Δ

    কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ 16 // swift-tools-version: 5.8 import PackageDescription let package = Package( name: "MyPackage", // ... targets: [ .target( name: "MyTarget", swiftSettings: [ .enableUpcomingFeature("ExistentialAny", .when(configuration: .debug)), ] ), ] // ... )
  17. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 9DPEFϓϩδΣΫτͷλʔήοτʹద༻͢Δ

    কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ 17 9DPEFͷεΫϦʔϯγϣοτʢ-enable-upcoming-featureΛ࢖͏ʹ͸4XJGUʢ9DPEFʣҎ͕߱ඞཁ͕ͩɺݱ࣌఺ͰCFUB൛ͷͨΊ୅༻ʣ
  18. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 9DPEFϓϩδΣΫτͷλʔήοτʹద༻͢Δ

    কདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ 18 9DPEFͷεΫϦʔϯγϣοτʢ-enable-upcoming-featureΛ࢖͏ʹ͸4XJGUʢ9DPEFʣҎ͕߱ඞཁ͕ͩɺݱ࣌఺ͰCFUB൛ͷͨΊ୅༻ʣ
  19. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. 9DPEF͕ఏҊͨ͠௨ΓʹҰؾʹमਖ਼͢Δ

    ͓·͚ w ϝχϡʔͷ 
 ʮ&EJUPSʯʮ'JY"MM*TTVFTʯͰ Ұؾʹमਖ਼ఏҊΛ൓өͰ͖Δ w ࠓճͷ&YJTUFOUJBM"OZͷྫͰ͸ɺ 9DPEFͷఏҊ͕ 
 ޡ͍ͬͯΔ৔߹͕͋Δ 
 ʢ࠶౓ίϯύΠϧΤϥʔʹͳΔʣ 19 9DPEFͷεΫϦʔϯγϣοτ
  20. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ·ͱΊ

    ଘࡏܕʹʮBOZʯΛ෇͚͍ͯͳ͍ͱ͖ʹίϯύΠϧΤϥʔʹ͢Δ w 4XJGU͔Βʮকདྷ௥Ճ༧ఆͷݴޠվળ΁ͷஈ֊తͳద༻ʯΛߦ͏ͨΊͷ ίϯύΠϥϑϥάʢ-enable-upcoming-feature XʣͳͲ͕௥Ճ͞ΕΔ w 4XJGU1BDLBHFͷλʔήοτ SwiftSetting.enableUpcomingFeature(_:_:) Ͱnameʹ"ExistentialAny"Λࢦఆ͢Δ w 9DPEFϓϩδΣΫτͷλʔήοτ 
 ʮ4XJGU$PNQJMFS$VTUPN'MBHTʯʮ0UIFS4XJGU'MBHT ʢOTHER_SWIFT_FLAGSʣʯʹ 
 -enable-upcoming-feature ExistentialAnyΛ௥Ճ͢Δ 20
  21. Copyright © 2023 treastrain / Tanaka RyogaɹAll rights reserved. ɹɹɹɹɹεϥΠυɾൃද಺༰͸IUUQTUSFUKQͰެ։͍ͯ͠·͢ɹ⏩

    21 ଘࡏܕʹʮBOZʯΛ෇͚͍ͯͳ͍ͱ͖ʹ 
 ίϯύΠϧΤϥʔʹ͢Δ 4&1JFDFNFBMBEPQUJPOPGVQDPNJOHMBOHVBHF JNQSPWFNFOUTͷ࢖༻ྫ potatotips #81 
 iOS / Android ։ൃ Tips ڞ༗ձ #potatotips