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

Swift 4を振り返り、Swift 5の夢を見る

7ddcca09c00a2744b983974225447d19?s=47 Sho Ikeda
October 30, 2017

Swift 4を振り返り、Swift 5の夢を見る

Hatena Engineer Seminar #9 @ Tokyoでの発表資料です #hatenatech

https://hatena.connpass.com/event/69844/

7ddcca09c00a2744b983974225447d19?s=128

Sho Ikeda

October 30, 2017
Tweet

Transcript

  1. Swift 4ΛৼΓฦΓ Swift 5ͷເΛݟΔ id:ikesyo Hatena Engineer Seminar #9 @

    Tokyo 2017-10-30 Mon #hatenatech 1
  2. id:ikesyo • ͍͚͠ΐʔʗ஑ా ᠳ • גࣜձࣾ͸ͯͳ • ϒοΫϚʔΫΞϓϦνʔϜ • Swiftίϛολʔ

    • ओઓ৔͸swift-corelibs-foundation • http://b.hatena.ne.jp/ikesyo/ • https://twitter.com/ikesyo • https://github.com/ikesyo 2
  3. Agenda • Swift 4.0 • Swift 4.1 • Swift 5.0

    3
  4. Swift 4.0 4

  5. Swift 4.0 https://swift.org/blog/swift-4-0-released/ • September 2017 • ABI Stability is

    deferred to Swift 5 • Two language modes • Swift 3.2 • Swift 4.0 5
  6. Swift 4.0 https://swift.org/blog/swift-source-compatibility-test-suite/ • Source Compatibility Test Suite • https://swift.org/source-compatibility/

    • https://github.com/apple/swift-source-compat-suite 6
  7. Swift 4.0 • 30 proposals are implemented • https://apple.github.io/swift- evolution/#?version=4

    • And SE-0161 • Smart KeyPaths: Better Key-Value Coding for Swift 7
  8. Codable • SE-0166 Swift Archival & Serialization • Automatic requirements

    synthesis (can be overridden by a manual implementation) • DerivedConformanceCodable.cpp • DerivedConformanceCodingKey.cpp • SE-0167 Swift Encoders 8
  9. Codable struct User: Codable { let id: String let name:

    String let age: Int } 9
  10. Codable let json = """ { "id": "123", "name": "Foo

    Bar", "age": 24 } """.data(using: .utf8)! let decoder = JSONDecoder() let decodedUser = try decoder.decode(User.self, from: json) let encoder = JSONEncoder() let encodedUserData: Data = try encoder.encode(decodedUser) 10
  11. Codable Decodable struct User: Codable { ... private enum CodingKeys:

    String, CodingKey { case id, name, age } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(String.self, forKey: .id) name = try container.decode(String.self, forKey: .name) age = try container.decode(Int.self, forKey: .age) } } 11
  12. Codable Encodable struct User: Codable { ... private enum CodingKeys:

    String, CodingKey { case id, name, age } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) try container.encode(age, forKey: .age) } } 12
  13. Codable • https://github.com/norio-nomura/ObjectEncoder • Swift Encoders implementation using [String: Any],

    [Any] or Any as payload. • May be useful for migrating from existing JSON decoders/mappers 13
  14. Smart KeyPaths • SE-0161 Smart KeyPaths: Better Key-Value Coding for

    Swift struct Presenter { var order: Int var name: String } var p = Presenter(order: 2, name: "ikesyo") p[keyPath: \Presenter.order] // 2 p[keyPath: \Presenter.name] = "஑ా ᠳ" 14
  15. Smart KeyPaths KeyPath chain struct Foo { let bar: Bar?

    } struct Bar { let baz: Baz } struct Baz { let value: Int } // Optional chaining \Foo.bar?.baz.value // KeyPath<Foo, Int?> 15
  16. Smart KeyPaths Class hierarchy class AnyKeyPath { ... } class

    PartialKeyPath<Root>: AnyKeyPath { ... } class KeyPath<Root, Value>: PartialKeyPath<Root> { ... } class WritableKeyPath<Root, Value>: KeyPath<Root, Value> { ... } class ReferenceWritableKeyPath<Root, Value>: WritableKeyPath<Root, Value> { ... } 16
  17. Smart KeyPaths • https://github.com/kishikawakatsumi/Kuery • https://github.com/kishikawakatsumi/RealmTypeSafeQuery • [SR-5220] Expose API

    to retrieve string representation of KeyPath - Swift • _kvcKeyPathString 17
  18. The Law of Exclusivity • SE-0176 Enforce Exclusive Access to

    Memory • Required for ABI Stability • Ownership Manifesto • @omochimetaru Swift ʹಋೖ༧ఆͷ Ownership ػೳͷ঺ հ - Qiita (Swift Tweets 2017 Fall) 18
  19. The Law of Exclusivity var x = 0, y =

    0 // NOT A CONFLICT. These two accesses to 'x' are both reads. // Each completes instantaneously, so the accesses do not overlap and // therefore do not conflict. Even if they were not instantaneous, they // are both reads and therefore do no conflict. let z = x + x // NOT A CONFLICT. The right-hand side of the assignment is a read of // 'x' which completes instantaneously. The assignment is a write to 'x' // which completes instantaneously. The accesses do not overlap and // therefore do not conflict. x = x 19
  20. The Law of Exclusivity // NOT A CONFLICT. The right-hand

    side is a read of 'x' which completes // instantaneously. Calling the operator involves passing 'x' as an inout // argument; this is a write access for the duration of the call, but it does // not begin until immediately before the call, after the right-hand side is // fully evaluated. Therefore the accesses do not overlap and do not conflict. x += x // CONFLICT. Passing 'x' as an inout argument is a write access for the // duration of the call. Passing the same variable twice means performing // two overlapping write accesses to that variable, which therefore conflict. swap(&x, &x) 20
  21. The Law of Exclusivity extension Int { mutating func assignResultOf(_

    function: () -> Int) { self = function() } } // CONFLICT. Calling a mutating method on a value type is a write access // that lasts for the duration of the method. The read of 'x' in the closure // is evaluated while the method is executing, which means it overlaps // the method's formal access to 'x'. Therefore these accesses conflict. x.assignResultOf { x + 1 } 21
  22. Swift 4.1 22

  23. Swift 4.1 https://swift.org/blog/swift-4-1-release-process/ Swift 4.1 is not binary compatible with

    4.0. It contains a variety of under-the-hood changes that are part of the effort to stabilize the Swift ABI in Swift 5. • The first half of 2018 23
  24. Implemented proposals (as of Oct 30) • SE-0075 Adding a

    Build Configuration Import Test #if canImport(UIKit) // UIKit-based code #elseif canImport(Cocoa) // OSX code #elseif // Workaround/text, whatever #endif 24
  25. Implemented proposals (as of Oct 30) • SE-0185 Synthesizing Equatable

    and Hashable conformance • DerivedConformanceEquatableHashable.cpp • SE-0186 Remove ownership keyword support in protocols 25
  26. Features that may be added • SE-0157 Support recursive constraints

    on associated types // Will not currently compile protocol Sequence { associatedtype SubSequence: Sequence where Iterator.Element == SubSequence.Iterator.Element, SubSequence.SubSequence == SubSequence // Returns a subsequence containing all but the first 'n' items // in the original sequence. func dropFirst(_ n: Int) -> Self.SubSequence // ... } 26
  27. Features that may be added • SE-0143 Conditional conformances •

    Generics Manifesto extension Array: Equatable where Element: Equatable { static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... } } 27
  28. Swift 5.0 28

  29. Swift 5.0 https://github.com/apple/swift-evolution/blob/master/README.md • Expected release date: Late 2018 •

    Primary Focus: ABI Stability • Swift ABI Stability Manifesto • ABI Dashboard • Generics features needed for standard library, API resilience, Memory ownership model (carried over from Swift 4) 29
  30. 30

  31. Swift 5.0 • Other Improvements • String ergonomics • Improvements

    to existing standard library facilities • Foundation improvements • Syntactic additions • Laying groundwork for a new concurrency model 31
  32. String ergonomics • String Manifesto String re-evaluation: String is one

    of the most important fundamental types in the language. The standard library leads have numerous ideas of how to improve the programming model for it, without jeopardizing the goals of providing a unicode-correct-by-default model. Our goal is to be better at string processing than Perl! 32
  33. Laying groundwork for a new concurrency model • Async/Await for

    Swift • https://gist.github.com/lattner/ 429b9070918248274f25b714dcfc7619 • @koher Proposalʹ͸ࡌ͍ͬͯͳ͍Swift 5ͷasync/await͕ૉ੖Β͍͠ͱ ࢥ͏ཧ࿦తഎܠ - Qiita (Swift Tweets 2017 Fall) • Concurrency in Swift: One approach • https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782 33
  34. Proposals (as of Oct 30) These seem to be related

    to ABI stability and API resilience. • [Proposal] Non-exhaustive enums #751 • [Proposal] Restrict cross-module struct initializers #753 • [Proposal] Cross-module inlining and specialization #754 34
  35. Appendix • ࣮ફSwiftίϯύΠϥ #swtws - Qiita (Swift Tweets 2017 Summer)

    • ೔ຊਓॳͷSwiftίϛολʔͰ͋Δ@rintaro͞ΜʹΑΔSwift ίϯύΠϥ΁ͷίϯτϦϏϡʔτํ๏ͷղઆ • Swift Tweets 2018 Winter @ 2018-01-20 Sat • @ikesyo΋ൃද͠·͢ 35
  36. ! Happy Swift Coding!! ! 36

  37. Thank you❗" 37