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

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

Sho Ikeda
October 30, 2017

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

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

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

Sho Ikeda

October 30, 2017
Tweet

More Decks by Sho Ikeda

Other Decks in Programming

Transcript

  1. id:ikesyo • ͍͚͠ΐʔʗ஑ా ᠳ • גࣜձࣾ͸ͯͳ • ϒοΫϚʔΫΞϓϦνʔϜ • Swiftίϛολʔ

    • ओઓ৔͸swift-corelibs-foundation • http://b.hatena.ne.jp/ikesyo/ • https://twitter.com/ikesyo • https://github.com/ikesyo 2
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. Implemented proposals (as of Oct 30) • SE-0185 Synthesizing Equatable

    and Hashable conformance • DerivedConformanceEquatableHashable.cpp • SE-0186 Remove ownership keyword support in protocols 25
  19. 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
  20. 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
  21. 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
  22. 30

  23. 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
  24. 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
  25. 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
  26. 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
  27. Appendix • ࣮ફSwiftίϯύΠϥ #swtws - Qiita (Swift Tweets 2017 Summer)

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