BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

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

This item in japanese

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.

Rate this Article

Adoption
Style

BT