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

"What's new in Swift"の要約 / swift_5_7_summary

42a6a049ac8f5265f31858a9509217fb?s=47 uhooi
June 24, 2022

"What's new in Swift"の要約 / swift_5_7_summary

集まれSwift好き!Swift愛好会スピンオフ WWDC22セッション要約会 @オンライン
https://love-swift.connpass.com/event/247317/

## 参考リンク

- 個人まとめ
https://github.com/uhooi/mobile-app-trends/blob/main/2022/wwdc22.md#swift-57

- Xcode 14 Beta リリースノート
https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

- Swift 5.7 チェンジログ
https://github.com/apple/swift/blob/6cc83594604e1a11f3bdece5ad6a4370e8c0dee3/CHANGELOG.md#swift-57

- What's new in Swift - WWDC22 - Videos - Apple Developer
https://developer.apple.com/videos/play/wwdc2022/110354/

- SE-0345: `if let` shorthand for shadowing an existing optional variable
https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md

- SE-0326: Enable multi-statement closure parameter/result type inference
https://github.com/apple/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md

- SE-0347: Type inference from default expressions
https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md

- SE-0309: Unlock existentials for all protocols
https://github.com/apple/swift-evolution/blob/main/proposals/0309-unlock-existential-types-for-all-protocols.md

- SE-0346: Lightweight same-type requirements for primary associated types
https://github.com/apple/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md

- SE-0341: Opaque Parameter Declarations
https://github.com/apple/swift-evolution/blob/main/proposals/0341-opaque-parameters.md

42a6a049ac8f5265f31858a9509217fb?s=128

uhooi

June 24, 2022
Tweet

More Decks by uhooi

Other Decks in Programming

Transcript

  1.  ۚ  ू·Ε4XJGU޷͖ʂ4XJGUѪ޷ձεϐϯΦϑ88%$ηογϣϯཁ໿ձ!ΦϯϥΠϯ !UIF@VIPPJ 8IBUTOFXJO4XJGUͷ ཁ໿💡

  2. ΢ϗʔΠ J04ΞϓϦΤϯδχΞ w ͱ͖Ͳ͖4XJGUѪ޷ձͷ 
 ӡӦʹࢀՃ͍ͯ͠Δ w ೥࿈ଓճ໨ w ݄ʹస৬ͨ͠

  3. ͷ৽ػೳʢશൠɾδΣωϦΫεʣʹ͍ͭͯ࿩͠·͢ 4XJGU

  4. શൠ

  5. 4& JGMFUߏจΛলུͯ͠ॻ͚Δ let foo: Int? = 0 let bar: String?

    = "bar" // Swift 5.6 if let foo = foo { ... } guard let bar = bar else { ... }
  6. 4& JGMFUߏจΛলུͯ͠ॻ͚Δ let foo: Int? = 0 let bar: String?

    = "bar" // Swift 5.7 if let foo { ... } guard let bar else { ... }
  7. 4& Ϋϩʔδϟ಺͕ෳ਺ͷจͰ΋ɺҾ਺ͱ໭Γ஋ͷܕΛলུͰ͖Δ func map<T>(fn: (Int) -> T) -> T {

    fn(42) } // Swift 5.6 _ = map { value -> Double in print("Foo") return Double(value) }
  8. 4& Ϋϩʔδϟ಺͕ෳ਺ͷจͰ΋ɺҾ਺ͱ໭Γ஋ͷܕΛলུͰ͖Δ func map<T>(fn: (Int) -> T) -> T {

    fn(42) } // Swift 5.7 _ = map { print("Foo") return Double(value) }
  9. δΣωϦΫε

  10. 4& δΣωϦοΫؔ਺ͷҾ਺ʹσϑΥϧτ஋ΛࢦఆͰ͖Δ struct Foo: Equatable { static let shared =

    Foo() } // Swift 5.6 final class Bar<F: Equatable> { private let foo: F init(foo: F) { self.foo = foo } convenience init() where F == Foo { self.init(foo: .shared) } }
  11. 4& δΣωϦοΫؔ਺ͷҾ਺ʹσϑΥϧτ஋ΛࢦఆͰ͖Δ struct Foo: Equatable { static let shared =

    Foo() } // Swift 5.7 final class Bar<F: Equatable> { private let foo: F init(foo: F = Foo.shared) { self.foo = foo } }
  12. 4& ࿈૝ܕΛ࣋ͭϓϩτίϧΛܕͱͯ͠࢖͑Δ // Swift 5.6 protocol Foo {} protocol Bar

    { associatedtype FooType: Foo func foo() -> FooType } struct FooStruct: Foo {} struct BarStruct: Bar { func foo() -> FooStruct { .init() } } struct AnyBar<B: Bar> { private let _foo: () -> B.FooType init(_ base: B) { _foo = { base.foo() } } } extension AnyBar: Bar { func foo() -> B.FooType { _foo() } } let bar: AnyBar = .init(BarStruct()) _ = bar.foo()
  13. 4& ࿈૝ܕΛ࣋ͭϓϩτίϧΛܕͱͯ͠࢖͑Δ let bar: any Bar = BarStruct() _ =

    bar.foo() // Swift 5.7 protocol Foo {} protocol Bar { associatedtype FooType: Foo func foo() -> FooType } struct FooStruct: Foo {} struct BarStruct: Bar { func foo() -> FooStruct { .init() } }
  14. 4& ؔ਺ͷҾ਺ʹsome͕࢖͑Δ // Swift 5.6 func foo<V1: View, V2: View>(_

    v1: V1, _ v2: V2) -> some View { // ... }
  15. 4& ؔ਺ͷҾ਺ʹsome͕࢖͑Δ // Swift 5.7 func foo(_ v1: some View,

    _ v2: some View) -> some View { // ... }
  16. 4& ϓϩτίϧʹࢁΧοίͰؔ࿈ܕΛॻ͚Δ // Swift 5.6 protocol Graph { associatedtype Foo

    associatedtype Bar } struct GraphStruct: Graph { typealias Foo = Int typealias Bar = String } protocol TestProtocol { func bar<G, F, B>(_: G, foo: F) -> [B] where G: Graph, G.Foo == F, G.Bar == B } extension Graph where Foo == Int, Bar == String { // ... }
  17. 4& ϓϩτίϧʹࢁΧοίͰؔ࿈ܕΛॻ͚Δ // Swift 5.7 protocol Graph<Foo, Bar> { associatedtype

    Foo associatedtype Bar } struct GraphStruct: Graph { typealias Foo = Int typealias Bar = String } protocol TestProtocol { func bar<F, B>(_: some Graph<F, B>, foo: F) -> [B] } extension Graph<Int, String> { // ... } final class Test { // Swift 5.6 Ͱ͸࣮ݱͰ͖ͳ͍ func build() -> some Graph<Int, String> { GraphStruct() } }
  18. ·ͱΊ w ίʔυͷهड़ྔ͕ݮΔݴޠ࢓༷͕૿͑ͨ w δΣωϦΫε͕ڧԽ͞ΕɺΑΓ௚ײతʹهड़Ͱ͖ΔΑ͏ʹͳͬͨ w someͱanyΛ࢖͏ͱಡΈ΍͍͢