What’s new in swift 4.2?

WWDC18

Source Compatibility: One Compiler with Three Modes

Source Compatibility

Swift 3 : Accepts Swift 3 code that built with Xcode 8
Swift 4 : Accepts Swift 4 code that built with Xcode 9.3
Swift 4.2 : Same as with Swift 4 but incorporates new Swift-related SDK . improvements

Faster Swift Debug Builds

Swift Debug Builds
Build speeds of Xcode 10 compared to Xcode 9

swift debug builds

Compilation Mode versus Optimization Level

compilation maode

Stop Using Debug with Whole Module Compilation

Module compilation

Using Whole Module for Debug builds was a stopgap to improve builds Whole Module prevents incremental builds
 Use Incremental for Debug builds!

Runtime Optimizations

// Calling Convention: “Owned” (+1 retain)

class X { … }

func caller() {

// ‘x’ created with +1 reference count

let x = X()

}

foo(x)

func foo(x: X) {

let y = x.value
// Calling Convention: “Owned” (+1 retain)
class X { ... }
func caller() {
// ‘x’ created with +1 reference count
let x = X()
foo(x)

}

func foo(x: X) {

let y = x.value
...
// release x
}

Collection of Enum Cases

// SE-0194 Derived Collection of Enum Cases
enum Gait {
  case walk
  case trot
  case canter 
  case gallop
  case jog  
static var allCases: [Gait] = [.walk, .trot, .canter, .gallop]

}

for gait in Gait.allCases {  //This code will never print “jog”!  

print(gait)

}

// SE-0194 Derived Collection of Enum Cases with using CaseIterable

enum Gait: CaseIterable {
  case walk
  case trot
  case canter 
  case gallop
  case jog
}

for gait in Gait.allCases {  

print(gait) // It will print all cases

}

Conditional Conformance

Inconsistent Behavior in Swift 4.0

Why Aren’t All Arrays Equatable?

Conditional Conformance

Conditional Conformance Allows Composition

Synthesized Equatable and Hashable

// SE-0185 Synthesizing Equatable and Hashable Conformance

struct Restaurant: Equatable {  
   let name: String
   let hasTableService: Bool
   let kidFriendly: Bool
}

// SE-0185 Synthesizing Equatable and Hashable Conformance

struct Restaurant: Hashable {  

   let name: String
   let hasTableService: Bool
   let kidFriendly: Bool
}

// Synthesizing Conditional Equatable and Hashable

enum Either<Left, Right> {  
   case left(Left)
   case right(Right)
}

extension Either: Equatable where Left: Equatable, Right: Equatable { }
extension Either: Hashable where Left: Hashable, Right: Hashable { }

// This just works!
var mySet = Set<Either<Int, String>>()