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

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

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

uhooi

June 24, 2022
Tweet

More Decks by uhooi

Other Decks in Programming

Transcript

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

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

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

    fn(42) } // Swift 5.6 _ = map { value -> Double in print("Foo") return Double(value) }
  4. 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) } }
  5. 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 } }
  6. 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()
  7. 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() } }
  8. 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 { // ... }
  9. 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() } }