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

What's new in Swift 5?

Eeb061c8b2816b771920da1b3e7904a3?s=47 Swift India
February 09, 2019

What's new in Swift 5?

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

February 09, 2019
Tweet

Transcript

  1. Vatsal Manot Software engineer at Pulse getpulse.care

  2. What’s new in Swift 5? A brief overview.

  3. None
  4. Swift 5 • Maturity - less ambitious, more practical •

    Stability • Balance between correctness and usability • Vastly improved flexibility and expressiveness • Exposing optimization “magic” • Enhancements to the standard library
  5. What is new? • ABI stability • String interpolation revamp

    • Result type • @dynamicCallable • Simplification of protocols such as Sequence and Collection • Cool optimization attributes (@inlinable, @usableFromInline etc.) • Enhancements to math types/protocols (+ SIMD vectors) • … and a bunch of other stuff
  6. Swift 5… or 4.2? • @dynamicMemberLookup • @inlinable • CaseIterable

    • Adding ‘toggle’ to Bool (they spent weeks discussing this ಠ_ಠ)
  7. Let’s talk ABI • ABI stands for “Application Binary Interface”

    • It is a specification. • Things this specification describes:
 - Calling conventions
 - Type metadata
 - Name mangling
 - Runtime APIs
 - Standard library APIs
  8. What does this mean? • Reduced binary sizes (yay!) •

    iOS 12.2 will ship with Swift 5 • About 7-10MB size reduction • Widespread usage of Swift throughout the OS in the near future • Swift-ier APIs for cocoa/touch frameworks (hopefully)
  9. From the Swift Blog Image from https://swift.org/blog/abi-stability-and-more/

  10. Stability • ABI stability - binary compatibility • Module stability

    - source compatibility • Library evolution support (aka resilience) - things like inlining/non-exhaustive enumerations
  11. Image from https://swift.org/blog/abi-stability-and-more/

  12. Inlining, module files and SIL • @inlinable exports the body

    of the function • Exported functions are stored as SIL in the module file • SIL stands for Swift Intermediate Language • “A Swift module file is a binary serialization of the AST- level declarations from one or more source files, together with the SIL instructions from the bodies of any inlinable functions.” - Slava Pestov
  13. Inlining and resilience struct Foo { @inlinable public func bar()

    { // does x } } struct Foo { @inlinable public func bar() { // does x faster } } to…
  14. Inlining and resilience struct Foo { @inlinable public func bar()

    { // does x } } struct Foo { @inlinable public func bar() { // does y } } to…
  15. Specialization

  16. Benefits • An order of magnitude faster • Protocol dispatch

    vs static dispatch • Compiler can optimize floating point operations using SIMD • Code remains generic
  17. @inlinable • Should be used judiciously • Good for framework

    developers • Good for well defined algorithms • Bad for changing implementations • Bad for debugging (obfuscated stack trace)
  18. String Interpolation

  19. String interpolation ExpressibleByStringInterpolation DefaultStringInterpolation ??? ??? ??? String formatting UTF8,

    UTF16, UTF32, WTF? Unicode? Regex
  20. None
  21. ExpressibleByStringInterpolation • Aka “variable substitution” • Evaluating expressions/placeholders in string

    literals • Can apply to types that are NOT String/Substring • Useful for constructing DSLs, complex queries, formatting strings
  22. None
  23. StringInterpolationProtocol • Scratchpad for constructing a type from interpolated segments

    • Standard library provides a default implementation • Default implementation is… DefaultStringInterpolation • You can create custom interpolation structures • Custom interpolation structures can capture richer information
  24. Use case: Formatting extension DefaultStringInterpolation { mutating func appendInterpolation(_ double:

    Double, precision: Int) { appendInterpolation(String(format: "%.\(precision)f", double)) } }
  25. Formatting dates to…

  26. Use case: Regex extension DefaultStringInterpolation { mutating func appendInterpolation(oneOf strings:

    String...) { appendInterpolation("(?:\(strings.joined(separator: "|")))") } } var regex = "I like \(oneOf: "cats", "dogs", "parrots")" var string = "I like dogs" string.range(of: regex, options: .regularExpression) != nil // true var regex = "I like (?:cats|dogs|parrots)" var string = "I like dogs" string.range(of: regex, options: .regularExpression) != nil // true to…
  27. In the works… • Swift concurrency manifesto - first class

    concurrency support • Swift memory ownership manifesto - highly optimized memory management • Swift generics manifesto - vastly richer, more expressive type system
  28. Personal Favorites • SE-0193 Cross-module inlining and specialization • SE-0194

    Derived Collection of Enum Cases • SE-0212 Compiler Version Directive • SE-0229 SIMD Vectors • SE-0233 Make Numeric Refine a new AdditiveArithmetic Protocol
  29. Thank you!

  30. @vatsal_manot