BT

InfoQ Homepage News Swift 5.4 Brings Support for Multiple Variadic Arguments, Result Builders, and More

Swift 5.4 Brings Support for Multiple Variadic Arguments, Result Builders, and More

Bookmarks

Recently released Swift 5.4 brings support for multiple variadic parameters, extends implicit member syntax, adds support for local functions overloading, and more. Additionally, it improves runtime performance and binary size.

Prior to version 5.3, Swift allowed only one variadic parameter per parameter list. For example, this is how you defined a Swift function taking a variadic argument followed by a non-variadic one:

func variadicFunction(_ args: String..., additionalArg: String) {
  for arg in args {
    ...
  }
}

In Swift 5.4, you can now define more than one variadic argument, provided each variadic argument following the first one has a label:

func variadicFunction(_ args: String..., moreArgs: String...) {
  ...
}

Swift 5.4 also extends so-called "leading dot syntax" to chains of member references. Leading dot syntax allows to omit type information that can be inferred by the context, as in the following snippet:

view.backgroundColor = .systemBackground
//-- same as view.backgroundColor = UIColor.systemBackground

Until now, this syntax was only allowed to access static members. Now, you can chain multiple member accesses, including non-static members:

let milky: UIColor = .white.withAlphaComponent(0.5)
let milky2: UIColor = .init(named: "white")!.withAlphaComponent(0.5)
let milkyChance: UIColor? = .init(named: "white")?.withAlphaComponent(0.5)

Another new feature in Swift 5.4 is result builders, which makes it possible to define a function that implicitly builds up a result value from a sequence of components. For example, the declaration:

@TupleBuilder
func build() -> (Int, Int, Int) {
  1
  2
  3
}

will be interpreted as:

func build() -> (Int, Int, Int) {
  let _a = TupleBuilder.buildExpression(1)
  let _b = TupleBuilder.buildExpression(2)
  let _c = TupleBuilder.buildExpression(3)
  return TupleBuilder.buildBlock(_a, _b, _c)
}

According to Apple,

this proposal allows the creation of a new class of embedded domain-specific languages in Swift by applying builder transformations to the statements of a function.

Result builders are most famously used in Swift UI, but they have been also used to build various DSLs, such as a Shortcut DSL, a CSS DSL, and more.

As a final note about new syntax features available in Swift 5.4, it is worth mentioning local function overloading, which enables the definition of multiple embedded functions only differing from one another in their parameter list:

func aGlobalFunction() {

    func localFunc(arg: Int) { ... }
    func localFunc(arg: String) { ... }
    func localFunc(arg: Float) { ... }

    ...
}

A welcome improvement brought by Swift 5.4 is improved performance when checking protocol conformance at runtime. This will make casting using as? or as! much faster. Additionally, String interpolation andretain/release calls have also become more efficient.

Swift 5.4 includes many more improvements than can be covered here, including a number of new features in the Swift Package Manager, extended Windows support, and improved developer experience. Do not miss the official announcement if interested in the full details.

Swift 5.4 is included in Xcode 12.5, which is available on the Mac App Store.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.