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. ۚ

    ू·Ε4XJGU޷͖ʂ4XJGUѪ޷ձεϐϯΦϑ88%$ηογϣϯཁ໿ձ!ΦϯϥΠϯ
    [email protected]
    8IBUTOFXJO4XJGUͷ
    ཁ໿💡

    View Slide

  2. ΢ϗʔΠ
    J04ΞϓϦΤϯδχΞ
    w ͱ͖Ͳ͖4XJGUѪ޷ձͷ

    ӡӦʹࢀՃ͍ͯ͠Δ
    w ೥࿈ଓճ໨
    w ݄ʹస৬ͨ͠

    View Slide

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

    View Slide

  4. શൠ

    View Slide

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


    let bar: String? = "bar"


    // Swift 5.6


    if let foo = foo { ... }


    guard let bar = bar else { ... }

    View Slide

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


    let bar: String? = "bar"


    // Swift 5.7


    if let foo { ... }


    guard let bar else { ... }

    View Slide

  7. 4&
    Ϋϩʔδϟ಺͕ෳ਺ͷจͰ΋ɺҾ਺ͱ໭Γ஋ͷܕΛলུͰ͖Δ
    func map(fn: (Int) -> T) -> T {


    fn(42)


    }


    // Swift 5.6


    _ = map { value -> Double in


    print("Foo")


    return Double(value)


    }

    View Slide

  8. 4&
    Ϋϩʔδϟ಺͕ෳ਺ͷจͰ΋ɺҾ਺ͱ໭Γ஋ͷܕΛলུͰ͖Δ
    func map(fn: (Int) -> T) -> T {


    fn(42)


    }


    // Swift 5.7


    _ = map {


    print("Foo")


    return Double(value)


    }

    View Slide

  9. δΣωϦΫε

    View Slide

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


    static let shared = Foo()


    }


    // Swift 5.6


    final class Bar {


    private let foo: F


    init(foo: F) {


    self.foo = foo


    }


    convenience init() where F == Foo {


    self.init(foo: .shared)


    }


    }

    View Slide

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


    static let shared = Foo()


    }


    // Swift 5.7


    final class Bar {


    private let foo: F


    init(foo: F = Foo.shared) {


    self.foo = foo


    }


    }

    View Slide

  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 {


    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()

    View Slide

  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()


    }


    }

    View Slide

  14. 4&
    ؔ਺ͷҾ਺ʹsome͕࢖͑Δ
    // Swift 5.6


    func foo(_ v1: V1, _ v2: V2) ->


    some View {


    // ...


    }

    View Slide

  15. 4&
    ؔ਺ͷҾ਺ʹsome͕࢖͑Δ
    // Swift 5.7


    func foo(_ v1: some View, _ v2: some View) ->


    some View {


    // ...


    }

    View Slide

  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, foo: F) -> [B]
    where G: Graph, G.Foo == F, G.Bar == B


    }


    extension Graph where Foo == Int, Bar ==
    String {


    // ...


    }

    View Slide

  17. 4&
    ϓϩτίϧʹࢁΧοίͰؔ࿈ܕΛॻ͚Δ
    // Swift 5.7


    protocol Graph {


    associatedtype Foo


    associatedtype Bar


    }


    struct GraphStruct: Graph {


    typealias Foo = Int


    typealias Bar = String


    }
    protocol TestProtocol {


    func bar(_: some Graph,
    foo: F) -> [B]


    }


    extension Graph {


    // ...


    }


    final class Test {


    // Swift 5.6 Ͱ͸࣮ݱͰ͖ͳ͍


    func build() -> some Graph
    {


    GraphStruct()


    }


    }

    View Slide

  18. ·ͱΊ
    w ίʔυͷهड़ྔ͕ݮΔݴޠ࢓༷͕૿͑ͨ
    w δΣωϦΫε͕ڧԽ͞ΕɺΑΓ௚ײతʹهड़Ͱ͖ΔΑ͏ʹͳͬͨ
    w someͱanyΛ࢖͏ͱಡΈ΍͍͢

    View Slide