Swift 5.9 programming language introduces macros and an ownership concept

Almost six months after Swift 5.8, version 5.9 of Apple’s open-source programming language was released. The current release introduces macros and has a bidirectional connection to C++. It also brings the first approaches to the ownership concept, which is intended to be an essential part of Swift 6.


Macros add previously defined content to parts of the source code before compilation. Swift’s implementation is based more on Rust’s macro system than on the simple macros in C or C++, where macros are simple placeholders for text modules that are replaced before compilation.

Swift 5.9 has two types of Markos: freestanding and attached. The former are with # excellent, and Swift calls the macro’s implementation when compiling. The following code uses this function()Macro from the standard Swift library:

func myFunction()
  print ("Das Programm befindet sich in der Funktion \(#function)")

The macro replaces #function by the function name, with which the above code outputs “The program is in the function myFunction()”.

Attached macros help, among other things, when working with Swift protocols, as the following code snippet from the Swift documentation shows OptionSet-Protocol automatically filled with a series of constants:

// Makro-Variante 
struct SundaeToppings 
  private enum Options: Int 
    case nuts
    case cherry
    case fudge

// Manuelle Umsetzung ohne Makros
struct SundaeToppings: OptionSet 
  let rawValue: Int
  static let nuts = SundaeToppings(rawValue: 1 << 0)
  static let cherry = SundaeToppings(rawValue: 1 << 1)
  static let fudge = SundaeToppings(rawValue: 1 << 2)

The Swift compiler does not simply replace the text of the macro, as with C or C++, but calls the respective implementation.

(Picture: Swift documentation)

Another important innovation is bidirectional interoperability with C++, which allows external functions to be used directly from Swift, as shown by an example from the Swift blog that uses the C++ function

// Clang module 'PromptResponder'
#pragma once
#include <vector>

std::vector<std::string> generatePromptResponse(std::string prompt);

from Swift calls:

import PromptResponder

let codeLines = 
  generatePromptResponse("Write Swift code that prints hello world")
  .filter  !$0.isEmpty 

for line in codeLines 

Swift 5.9 also introduces the first hints of an ownership concept for variables, which is not as strict and basic as in Rust, but should help optimize memory management. Among other things, this eliminates the need to count the individual references of the values. The new operator consume declares the lifetime of a variable to be over, so that when it is assigned, the old variable loses its validity. According to the instruction

let b = consume a

contains b the value of aand subsequent attempts a access trigger an error. The operator can also be used to create a variable

_ = consume a

explicitly release without assigning the value to another. Parameters can now as borrowing or consuming be declared. Additionally, Swift 5.9 non-copyable structs and enums a.

Other innovations in Swift 5.9 such as parameter packs for passing a flexible number of parameters and direct variable assignments in if and switch statements can be found in the Swift blog.

There is no information in the blog post about the further roadmap of the upcoming major version, which has already cast its shadow in Swift 5.8.


To home page

source site