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

roppongiswift6.pdf

E1cf27d9fa565b2867d171f21a39759c?s=47 tikidunpon
December 21, 2018

 roppongiswift6.pdf

「ROPPONGI.swift 第6回 望年会」での発表資料です。

E1cf27d9fa565b2867d171f21a39759c?s=128

tikidunpon

December 21, 2018
Tweet

Transcript

  1. Swift5ͷ৽ػೳʢൈਮʣ ROPPONGI.swift ୈ6ճ ๬೥ձ Koichi Tanaka @tikidunpon 2018/12/21

  2. Koichi Tanaka Origami inc. I love Swift/Go

  3. ே׆΍ͬͯ·͢ʢஶऀͰ͸ͳ͍Ͱ͢ɻʣ

  4. Agenda • ABI҆ఆԽͷঢ়گ • ֤Proposalͷઆ໌ʢશ෦͸ແཧͳͷͰൈਮʣ

  5. ABI҆ఆԽͷঢ়گ

  6. None
  7. ABI҆ఆԽͷঢ়گ • https://swift.org/abi-stability Ͱ֬ೝͰ͖Δ • υΩϡϝϯτ࡞੒Λ࢒ͯ͠΄΅׬͍ྃͯ͠ΔΑ͏ʹݟ͑Δ

  8. Data Layout • SR-3730 Create technical specification of the layout

    algorithms Type Metadata • SR-3731 Technical specification for the fixed part of the metadata layout of all language constructs Mangling • Done Calling Convention • SR-4349 Document the function signature lowering scheme
  9. ABI҆ఆԽͰΞϓϦαΠζ΋ݮΔ͔΋ʁ

  10. ֤Proposalͷઆ໌ʢൈਮʣ

  11. SE-0192 Handling Future Enum Cases

  12. কདྷ௥Ճ͞ΕΔέʔεͷϋϯυϦϯά • frozenʢ˞1ʣͱnon-frozenʢ˞2ʣͳEnumΛ۠ผ͢Δ • ϢʔβʔఆٛͷSwiftͷEnum͸frozenʹϚʔΫ͞ΕΔ • ࠓճͷมߋͰ͸Cݴޠ΍Appleͷඪ४ϥΠϒϥϦʹ͚ͩӨڹ • @unknownଐੑͷ௥Ճ ※1

    frozen͸ίʔυ͕ॻ͔Εͨ࣌఺Ͱcase͕௥Ճ͞Εͳ͍ ※2 non-frozen͸ίʔυ͕ॻ͔Εͨ࣌఺Ͱcase͕௥Ճ͞ΕΔ͔΋
  13. ԾʹiOSʹ͜Μͳenum͕͋Δͱ͖ʹ enum State { case foo case bar case baz

    }
  14. έʔεΛ໢ཏ͢ΔͳΒ͜͏ॻ͚·ͨ͠ func handle(state: State) { switch state { case .foo:

    print("do foo stuff") case .bar: print("do bar stuff") case .baz: print("do baz stuff") } }
  15. ͔͠͠iOSͷόʔδϣϯΞοϓͰenumʹέʔε͕௥Ճ͞Ε.. enum State { case foo case bar case baz

    case foobar //௥Ճ }
  16. ະఆٛͷέʔεfoobar͕ϋϯυϧ͞ΕΔͱΫϥογϡ͠·͢ func handle(state: State) { switch state { case .foo:

    print("do foo stuff") case .bar: print("do bar stuff") case .baz: print("do baz stuff") // Fatal error: unexpected enum case } }
  17. Swift5Ͱ@unknownଐੑ͕௥Ճ // কདྷ௥Ճ͞ΕΔະఆٛέʔε͸defaultΛ௨Δ // έʔεΛ໢ཏͯ͠ͳ͍ͱʮSwitch must be exhaustiveʯͷܯࠂ͕ग़Δҝɺ // ίϯύΠϧ࣌ʹcase͕૿͍͑ͯΔ͜ͱʹؾ͕͚ͭΔɻ

    // ͜ͷڍಈ͸ࣗ෼ͰNS_ENUMΛఆٛͯ͠importʢnon-frozenʣ͢Δͱ֬ೝͰ͖Δɻ func handle(state: State) { switch state { case .foo: print("do foo stuff") case .bar: print("do bar stuff") case .baz: print("do baz stuff") @unknown default: print("default") } }
  18. SE-0200 Enhancing String Literals Delimiters to Support Raw Text

  19. RawςΩετΛαϙʔτ͢ΔσϦϛλͷ௥Ճ • ͍··Ͱ͸\ΛΤεέʔϓ͢Δ࣌ʹ\\ͱॻ͔ͳ͍ͱ͍͚ͳ͍ • #͕σϦϛλͱͯ͠௥Ճ͞Εͨ • ੜσʔλΛίϐϖՄೳͱͳΓՄൖੑ͕ߴ·Δ

  20. //͍··Ͱ let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+" //Swift5 let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#

  21. SE-0211 Add Unicode Properties to Unicode.Scalar

  22. Unicode.Scalar΁Propertiesͷ௥Ճ • Scalar͕Uppercase͔൑ఆ͢Δํ๏͕ͳ͔ͬͨ • ࡉ͔͍UnicodeपΓͷॲཧΛ͢Δʹ͸ݱঢ়͸ICUʢ˞1ʣʹґଘ • ͕ͩICUΛ੩తʹϦϯΫ͠ͳ͍ͱϦδΣΫτ͞ΕΔʢ˞2ʣ ※1 ICU =

    International Components for Unicode ※2 Issues Linking with ICU
  23. Unicode.Scalar.PropertiesʹICUಉ༷ͷॲཧ͕ଟ͘௥Ճ let s: Unicode.Scalar = "9" print(s.properties.isASCIIHexDigit) //true let a:

    Unicode.Scalar = "a" print(a.properties.isLowercase) //true let emoji: Unicode.Scalar = " ! " print(emoji.properties.isEmoji) //true print(emoji.properties.isEmojiModifier) //false print(emoji.properties.isEmojiPresentation) //true let emojiModifier: Unicode.Scalar = "\u{1F3FB}" print(emojiModifier.properties.isEmojiModifier) //true
  24. SE-0213 Literal initialization via coercion

  25. ڧ੍తͳϦςϥϧॳظԽ • Swift4.2Ͱ͸A(B)ͱA.init(B)͕ඞͣ͠΋ಉ݁͡ՌͱͳΒͳ͍ • σϑΥϧτͷܕͰਪ࿦͞Εͯ͠·͏ • ظ଴͠ͳ͍݁Ռ΍Φʔόʔϑϩʔ͕ى͖ͨΓͨ͠

  26. Swift4.2 // ݁Ռ͕ҟͳΔʂ let c = 3.14159265358979323846 as Float80 //

    3.14159265358979323851 let d = Float80(3.14159265358979323846) // 3.141592653589793116
  27. Swift5 // ݁Ռ͕ಉ͡ʂ let c = 3.14159265358979323846 as Float80 //

    3.1415926535897932385 let d = Float80(3.14159265358979323846) // 3.1415926535897932385
  28. Swift5Ͱ͸A(B)ͷؔ਺ݺͼग़͠ͰB͕Ϧςϥϧࣜͷ࣌ɺ΋͠A͕ T.TypeΛ͍͔࣋ͬͯͯͭT͕Bʹରͯ͠ద੾ͳϦςϥϧϓϩτίϧ ʢExpressibleByStringLiteralͱ͔ʣʹ४ڌ͍ͯ͠Δ࣌A͸initΛ௚઀ ࢖ͬͯߏங͞ΕΔɻʢB as Aͱॻ͔Ε͍ͯΔ͔ͷΑ͏ʹʣ SE-0213 Detailed design͔ΒҾ༻຋༁

  29. SE-0218 Introduce compactMapValues to Dictionary by @d_date

  30. compactMapValuesΛDictionary΁௥Ճ

  31. compactMapValuesΛDictionary΁௥Ճ • mapValueͰdictionaryͷ஋͚ͩΛऔΓग़͢͜ͱ͸ग़དྷͨ

  32. compactMapValuesΛDictionary΁௥Ճ • mapValueͰdictionaryͷ஋͚ͩΛऔΓग़͢͜ͱ͸ग़དྷͨ • ஋ʹnilΛؚΉdictinaryͷ৔߹ਏ͔ͬͨ

  33. Swift4.2 //mapValueͰdictionaryͷ஋͚ͩΛऔΓग़ͯ͠ૢ࡞͕Մೳ let a = ["John": 1, "Bob": 2, "Alice":

    3] let b = a.mapValues{ $0 * 2 } //b = ["Bob": 4, "Alice": 6, "John": 2] ॱং͸ෆఆ // optionalΛ஋ʹ΋ͭdictionary let d: [String: String?] = ["a": "1", "b": nil, "c": "3"] let r1 = d.filter { $0.value != nil }.mapValues { $0! } let r2 = d.reduce(into: [String: String]()) { (result, item) in result[item.key] = item.value } // r1 == r2 == ["a": "1", "c": "3"]
  34. Swift5 // optionalΛ஋ʹ΋ͭdictionary let d: [String: String?] = ["a": "1",

    "b": nil, "c": "3"] let r3 = d.compactMapValues{ $0 } // r3 = ["c": "3", "a": "1"] ॱং͸ෆఆ
  35. SE-0235 Add Result to the Standard Library

  36. ඪ४ϥΠϒϥϦʹResultܕ͕௥Ճ͞Εͨ • ੒ޭ͔ࣦഊΛදݱ͢Δܕ • ಛʹඇಉظॲཧͷΤϥʔΛѻ͍ͮΒ͔ͬͨ • ͜Ε·Ͱantitypical/Result͕ྑ͘࢖ΘΕ͍ͯͨ

  37. ͍··ͰʢProposal͔ΒҾ༻ʣ URLSession.shared.dataTask(with: url) { (data, response, error) in guard error

    != nil else { self.handleError(error!) } guard let data = data, let response = response else { return // Impossible? } handleResponse(response, data: data) }
  38. Result͕ೖͬͨΒ͜ΜͳΠϝʔδʢProposal͔ΒҾ༻ʣ ※ ·ͩURLSessionʹ͜ͷ࣮૷͸ͳ͍ URLSession.shared.dataTask(with: url) { (result: Result<(URLResponse, Data), Error>)

    in switch result { case .value(let response): handleResponse(response.0, data: response.1) case .error(let error): handleError(error) } }
  39. ݱ࣌఺ͰҎԼͷΑ͏ʹॻ͚Δ swift-5.0-DEVELOPMENT-SNAPSHOT-2018-12-20 enum Err: Error, Equatable { case err case

    derr } func notThrowing() throws -> String { return "string" } func throwing() throws -> String { throw Err.err } let result1 = Result { try throwing() } let result2 = Result { try notThrowing() } print(type(of: result1)) //Result<String, Error> print(type(of: result2)) //Result<String, Error> print(try result2.get()) //stringʢ੒ޭ஋ΛͱΔgetͱ͔ੜ͑ͯΔʣ
  40. SE-0215 Conform Never to Equatable and Hashable

  41. Never͕EquatableͱHashableʹ४ڌ • Never͸৚݅ͳ͠ʹΤϥʔ΍trapͤ͞Δɺຢ͸ऴྃ͠ͳ͍ؔ਺ͷ ໭Γ஋ʹར༻Մೳ • ྫ͑͹faitalErrorؔ਺ͷ໭Γ஋͸Never • ࣦഊ͠ͳ͍ॲཧʹ͸Result<T, Never>Λ࢖͍͍ͨ •

    Swift4.2Ͱ͸Result<T, Never>஋͸ൺֱग़དྷͳ͍
  42. Swift5Ͱ͸NeverΛؚΉResult͕ൺֱͰ͖Δ let r1: Result<String, Never> let r2: Result<String, Never> r1

    = Result.success("success") r2 = Result.success("success") if r1 == r2 { print("r1 == r2") }
  43. ·ͱΊʢײ૝ʣ • Ұ൪͸ABI҆ఆԽ • จࣈपΓڧԽ͞Εͨʢকདྷͷਖ਼نදݱαϙʔτʹ͍ۙͮͨʁʣ • ProposalಡΉͷ͸ָ͍͚͠Ͳແݶʹ༹͕͚࣌ؒΔ • iOS12 Programmingຊ͕ࣄલֶशʹྑ͔ͬͨ

  44. એ఻ʢஶऀͰ͸ͳ͍Ͱ͢ɻʣ

  45. ࢀߟࢿྉ • What’s new in Swift 5 by @d_date •

    What’s new in Swift 5.0 by @twostraws • swift-evolution#version=5 • Swift ABI Dashboard
  46. Thanks!! Koichi Tanaka @tikidunpon