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

What will be changed in Swift 4(2)

d_date
April 28, 2017

What will be changed in Swift 4(2)

2017/04/28 Swift愛好会 vol.18

d_date

April 28, 2017
Tweet

More Decks by d_date

Other Decks in Programming

Transcript

  1. •ϥϕϧ͸ίϯετϥΫλ໊ͷҰ෦ Expr.elet(locals: [], body: anExpr) // ⭕, Swift 3ͷํ๏ Expr.elet(locals:

    body:)([], anExpr) // ⭕, Swift 4͔ΒՄೳ // f(x: y:)(0, 0)ͱಉ͡ Expr.elet(locals: body:)(locals: 0, body: 0) // ❌ enum Expr { indirect case elet(locals: [(String, Expr)], body: Expr) }
  2. •ϥϕϧ͸λϓϧͷҰ෦Ͱ͸ͳ͍ enum Expr { indirect case elet(locals: [(String, Expr)], body:

    Expr) } let f = Expr.elet //([(String,Expr)],Expr) -> Expr f([], anExpr) // ⭕ f(locals: [], body: anExpr) // ❌ ม਺ʹ֨ೲ͢Δͱɺϥϕϧͷ৘ใ͕ফ͑Δ
  3. enum SyntaxTree { case type(variables: [TypeVariable]) case type(instantiated: [Type]) }

    case .type // ❌: ᐆດ case .type(variables: let variables) // ⭕:ϑϧωʔϜΛࢦఆ͢Δ • ڞ௨ͷϕʔεωʔϜΛ࢖͑Δ
  4. enum Animation { case fadeIn(duration: TimeInterval = 0.3) // ⭕

    } let anim = Animation.fadeIn() // ⭕ • Associated ValueʹσϑΥϧτ஋ΛઃఆͰ͖Δ
  5. enum Tree { case leaf() // (()) -> Tree }

    enum Tree { case leaf(Void) } •ۭͷλϓϧ͸௚઀ࢦఆͰ͖ͳ͘ͳͬͨ • Swift 4͔Β❌ • ্ͱಉ͜͡ͱΛ΍Δʹ͸ɺVoidΛॻ͘
  6. case .variable(name: let x) // ⭕ case .variable(x: let x)

    // ❌: `x`ͱ͍͏ϥϕϧ͸ͳ͍ case .lambda(parameters: let params, body: let body) // ⭕ case .lambda(params: let params, body: let body) // ❌: ୈҰҾ਺ϥϕϧ͕ؒҧ͍ͬͯΔ •ύλʔϯͷҰ؏ੑ indirect enum Expr { case variable(name: String) case lambda(parameters: [String], body: Expr) } ϥϕϧ໊͸౷Ұ͢Δඞཁ͕͋Δ
  7. case .variable(let name) // ⭕: ม਺໊͕ϥϕϧ໊ͱಉ͡ case .lambda(let parameters, let

    body) // ⭕: ม਺໊͕ϥϕϧ໊ͱಉ͡ case .variable(let x) // ❌: ϥϕϧͷॻ͖ํ͸1௨Γʹ౷Ұ͢Δඞཁ͕͋Δɻ case .lambda(let params, let body) // ❌: ্هͱಉ༷ case .lambda(parameters: let params, let body) // ❌ •ύλʔϯͷҰ؏ੑ indirect enum Expr { case variable(name: String) case lambda(parameters: [String], body: Expr) } ϥϕϧ໊ͱม਺໊͕ಉҰͷ࣌ʹݶΓɺϥϕϧΛ লུՄ
  8. [SE-0161] Smart KeyPaths: Better Key-Value Coding for Swift Accepted class

    Person { var name: String var friends: [Person] = [] var bestFriend: Person? = nil init(name: String) { self.name = name } } var han = Person(name: "Han Solo") var luke = Person(name: "Luke Skywalker") luke.friends.append(han)
  9. [SE-0161] Smart KeyPaths: Better Key-Value Coding for Swift Accepted //

    create a key path and use it let firstFriendsNameKeyPath = \Person.friends[0].name let firstFriend = luke[keyPath: firstFriendsNameKeyPath] // "Han Solo" // or equivalently, with type inferred from context luke[keyPath: \.friends[0].name] // "Han Solo" // rename Luke's first friend luke[keyPath: firstFriendsNameKeyPath] = "A Disreputable Smuggler" // optional properties work too let bestFriendsNameKeyPath = \Person.bestFriend?.name let bestFriendsName = luke[keyPath: bestFriendsNameKeyPath] // nil, if he is the last Jedi \Λ͚ͭΔ͜ͱͰɺKeyPathͱͯ͠࢖͏͜ͱ͕Ͱ͖Δ
  10. [SE-0163] String Revision: Collection Conformance, C Interop, Transcoding • StringΛBidirectionalCollection,

    RangeReplaceableCollection΁ͷద߹ • String.SubSequenceΛSubstringܕͱ͢Δ • Unicodeϓϩτίϧͷಋೖ • C interop ϝιου΁ͷ౷߹ • Transcoding Infrastructure ͷվగ •มߋޙ
  11. [SE-0167] Swift Encoders •௥Ճޙ 1. JSON / Property ListͷDecoder/Encoderͷ௥Ճ 2.

    NSKeyedArchiver/NSKeyedUnarchiverʹ CodableܕΛαϙʔτ
  12. CodableܕͱɺJSON Encoder/DecoderΛ૊Έ߹ΘͤΔ let farm = Farm(name: "Old MacDonald's Farm", location:

    Location(latitude: 51.621648, longitude: 0.269273), animals: [.chicken, .dog, .cow, .turkey, .dog, .chicken, .cow, .turkey, .dog]) let payload: Data = try JSONEncoder().encode(farm) do { let farm = try JSONDecoder().decode(Farm.self, from: payload) // Extracted as user types: let coordinates = "\(farm.location.latitude, farm.location.longitude)" } catch { // Encountered error during deserialization }
  13. [SE-0168] Multi-Line String Literals “””τϦϓϧΫΥʔτ”””Λ࢖͏͜ͱͰɺෳ਺ߦͷString Λ࢖͏͜ͱ͕Ͱ͖ɺ”ͷΤεέʔϓ͕ඞཁͳ͘ͳΔ assert( xml == """

    <?xml version="1.0"?> <catalog> <book id="bk101" empty=""> <author>\(author)</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </catalog> """ )
  14. [SE-0169] privateͱextensionͷ૬ޓ࡞༻ͷվળ •privateଐੑͰ΋ɺextensionΛ·͍ͨͰͷΞΫηε͕Մೳ struct S { private var p: Int

    func f() { use(g()) // ⭕:extensionʹએݴ͞Ε͍ͯΔg()ʹΞΫηεՄೳ } } extension S { private func g() { use(p) // ⭕:SͷextensionͳͷͰɺpʹΞΫηεͰ͖Δ } } extension S { func h() { use(g()) // ⭕:h()͸g()ʹΞΫηεͰ͖Δɻ } }
  15. // FileA.swift struct A { private var aMember : Int

    } // FileB.swift extension A { private func foo() { bar() // ⭕, foo()͸bar()΁ΞΫηεͰ͖Δ } } extension A { private func bar() { aMember = 42 // ❌, private͸ϑΝΠϧΛ·͍ͨͰͷΞΫηεෆՄ } } •privateʹ͸ϑΝΠϧΛ·͍ͨͰͷΞΫηε͸Ͱ͖ͳ͍
  16. [SE-0171] Reduce with inout •௥Ճޙ extension Sequence { func reduce<A>(into

    initial: A, _ combine: (inout A, Iterator.Element) - > ()) -> A { var result = initial for element in self { combine(&result, element) } return result } } inoutΛ࢖ͬͨreduce͕௥Ճ͞ΕΔ
  17. [SE-0171] Reduce with inout extension Sequence where Iterator.Element: Equatable {

    func uniq() -> [Iterator.Element] { return reduce(into: []) { (result: inout [Iterator.Element], element) in if result.last != element { result.append(element) } } } } ܭࢉྔ…O(n) •௥Ճޙ
  18. [SE-0171] Reduce with inout extension Sequence where Iterator.Element: Equatable {

    func uniq() -> [Iterator.Element] { return reduce([]) { (result: [Iterator.Element], element) in guard result.last != element else { return result } return result + [element] } } } ܭࢉྔ…O(n2) •௥Ճલ
  19. [SE-0172] One-sided Ranges •มߋޙ let greeting = s.prefix(upTo: i) let

    withComma = s.prefix(through: i) let location = s.suffix(from: i) ยଆͷΈͷrange operatorΛ௥Ճ let greeting = s[..<i]ɹ// ൒։۠ؒ let withComma = s[...i] // ด۠ؒ let location = s[i...] •มߋલ let greeting = s[s.startIndex..<i] //