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

What's new in Swift 5?

Swift India
February 09, 2019

What's new in Swift 5?

Swift India

February 09, 2019
Tweet

More Decks by Swift India

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Swift 5… or 4.2? • @dynamicMemberLookup • @inlinable • CaseIterable

    • Adding ‘toggle’ to Bool (they spent weeks discussing this ಠ_ಠ)
  4. 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
  5. 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)
  6. Stability • ABI stability - binary compatibility • Module stability

    - source compatibility • Library evolution support (aka resilience) - things like inlining/non-exhaustive enumerations
  7. 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
  8. Inlining and resilience struct Foo { @inlinable public func bar()

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

    { // does x } } struct Foo { @inlinable public func bar() { // does y } } to…
  10. Benefits • An order of magnitude faster • Protocol dispatch

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

    developers • Good for well defined algorithms • Bad for changing implementations • Bad for debugging (obfuscated stack trace)
  12. 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
  13. 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
  14. Use case: Formatting extension DefaultStringInterpolation { mutating func appendInterpolation(_ double:

    Double, precision: Int) { appendInterpolation(String(format: "%.\(precision)f", double)) } }
  15. 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…
  16. 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
  17. 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