What’s New in Swift 5.1?

Swift 5.1 is finally out! This article will take you through the advancements and changes the language has to offer in its latest version. By Cosmin Pupăză.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Overloading Functions With Any Parameters

Swift 5 prefers Any parameters instead of generic arguments for overloads of functions with only one parameter:

func showInfo(_: Any) -> String {
  return "Any value"
}

func showInfo<T>(_: T) -> String {
  return "Generic value"
}

showInfo("Swift 5")

showInfo() returns "Any value" in this case. Swift 5.1 works the other way round:

func showInfo(_: Any) -> String {
  "Any value"
}

func showInfo<T>(_: T) -> String {
  "Generic value"
}

showInfo("Swift 5.1")

showInfo() returns "Generic value" this time.

Type Aliases for Autoclosure Parameters

You can’t declare type aliases for @autoclosure parameters in Swift 5:

struct Closure<T> {
  func apply(closure: @autoclosure () -> T) {
    closure()
  }
}

apply(closure:) uses the autoclosure signature for closure in this case. You may use type aliases in the prototype of apply(closure:) in Swift 5.1:

struct Closure<T> {
  typealias ClosureType = () -> T

  func apply(closure:  @autoclosure ClosureType) {
    closure()
  }
}

apply(closure:) uses ClosureType for closure this time.

Returning Self From Objective-C methods

You have to inherit from NSObject if your class contains an @objc method which returns Self in Swift 5:

class Clone: NSObject {
  @objc func clone() -> Self {
    return self
  }
}

Clone extends NSObject because clone() returns Self. This is no longer the case in Swift 5.1:

class Clone {
  @objc func clone() -> Self {
    self
  }
}

Clone doesn’t have to inherit from anything this time.

Stable ABI Libraries

You use -enable-library-evolution in Swift 5.1 to make changes to library types without breaking its ABI. Structures and enumerations marked as @frozen can’t add, remove or reorder stored properties and cases [SE-0260].

Where to Go From Here?

You can download the final playground using the Download Materials link at the top or bottom of this tutorial.

Swift 5.1 adds many nice features to the ones already introduced in Swift 5. It also brings module stability to the language and implements complex paradigms used by the new frameworks introduced at WWDC like SwiftUI and Combine.

You can read more about the changes in this Swift version on the official Swift CHANGELOG or the Swift standard library differences.

You can also have a look at the Swift Evolution proposals to see what’s coming in the next version of Swift. Here, you can offer feedback for currently reviewed proposals and even pitch a proposal yourself!

What is your favorite Swift 5.1 feature so far? Let us know in the forum discussion below!