Share
Embed
What's new in Swift 5?
Swift India
February 09, 2019
Transcript
30 slides
-
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 -
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 -
Swift 5… or 4.2? • @dynamicMemberLookup • @inlinable • CaseIterable
• Adding ‘toggle’ to Bool (they spent weeks discussing this ಠ_ಠ) -
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 -
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) -
Stability • ABI stability - binary compatibility • Module stability
- source compatibility • Library evolution support (aka resilience) - things like inlining/non-exhaustive enumerations -
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 -
Inlining and resilience struct Foo { @inlinable public func bar()
{ // does x } } struct Foo { @inlinable public func bar() { // does x faster } } to… -
Inlining and resilience struct Foo { @inlinable public func bar()
{ // does x } } struct Foo { @inlinable public func bar() { // does y } } to… -
Benefits • An order of magnitude faster • Protocol dispatch
vs static dispatch • Compiler can optimize floating point operations using SIMD • Code remains generic -
@inlinable • Should be used judiciously • Good for framework
developers • Good for well defined algorithms • Bad for changing implementations • Bad for debugging (obfuscated stack trace) -
String interpolation ExpressibleByStringInterpolation DefaultStringInterpolation ??? ??? ??? String formatting UTF8,
UTF16, UTF32, WTF? Unicode? Regex -
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 -
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 -
Use case: Formatting extension DefaultStringInterpolation { mutating func appendInterpolation(_ double:
Double, precision: Int) { appendInterpolation(String(format: "%.\(precision)f", double)) } } -
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… -
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 -
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