Slide 1

Slide 1 text

Swift 4ΛৼΓฦΓ Swift 5ͷເΛݟΔ id:ikesyo Hatena Engineer Seminar #9 @ Tokyo 2017-10-30 Mon #hatenatech 1

Slide 2

Slide 2 text

id:ikesyo • ͍͚͠ΐʔʗ஑ా ᠳ • גࣜձࣾ͸ͯͳ • ϒοΫϚʔΫΞϓϦνʔϜ • Swiftίϛολʔ • ओઓ৔͸swift-corelibs-foundation • http://b.hatena.ne.jp/ikesyo/ • https://twitter.com/ikesyo • https://github.com/ikesyo 2

Slide 3

Slide 3 text

Agenda • Swift 4.0 • Swift 4.1 • Swift 5.0 3

Slide 4

Slide 4 text

Swift 4.0 4

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Codable struct User: Codable { let id: String let name: String let age: Int } 9

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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 15

Slide 16

Slide 16 text

Smart KeyPaths Class hierarchy class AnyKeyPath { ... } class PartialKeyPath: AnyKeyPath { ... } class KeyPath: PartialKeyPath { ... } class WritableKeyPath: KeyPath { ... } class ReferenceWritableKeyPath: WritableKeyPath { ... } 16

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Swift 4.1 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Implemented proposals (as of Oct 30) • SE-0185 Synthesizing Equatable and Hashable conformance • DerivedConformanceEquatableHashable.cpp • SE-0186 Remove ownership keyword support in protocols 25

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Features that may be added • SE-0143 Conditional conformances • Generics Manifesto extension Array: Equatable where Element: Equatable { static func ==(lhs: Array, rhs: Array) -> Bool { ... } } 27

Slide 28

Slide 28 text

Swift 5.0 28

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Appendix • ࣮ફSwiftίϯύΠϥ #swtws - Qiita (Swift Tweets 2017 Summer) • ೔ຊਓॳͷSwiftίϛολʔͰ͋Δ@rintaro͞ΜʹΑΔSwift ίϯύΠϥ΁ͷίϯτϦϏϡʔτํ๏ͷղઆ • Swift Tweets 2018 Winter @ 2018-01-20 Sat • @ikesyo΋ൃද͠·͢ 35

Slide 36

Slide 36 text

! Happy Swift Coding!! ! 36

Slide 37

Slide 37 text

Thank you❗" 37