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

What will be changed in Swift 4(1)

d_date
April 22, 2017

What will be changed in Swift 4(1)

2017/04/22 Cocoa関西

d_date

April 22, 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 ͷվగ •มߋޙ Accepted
  11. [SE-0168] Multi-Line String Literals Accepted “””τϦϓϧΫΥʔτ”””Λ࢖͏͜ͱͰɺෳ਺ߦͷ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> """ )
  12. [SE-0169] privateͱextensionͷ૬ޓ࡞༻ͷվળ Accepted •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()ʹΞΫηεͰ͖Δɻ } }
  13. // 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ʹ͸ϑΝΠϧΛ·͍ͨͰͷΞΫηε͸Ͱ͖ͳ͍