Swift 6.1-6.3 Features You Need to Know About

Syntax - Namespaces (Kinda) - Swift 6.3

An attempt to create namespaces is called Module name selectors in Swift 6.3

Swift 6.3 introduces module selectors to specify which imported module Swift should look in for an API used in your code. If you import more than one module that provides API with the same name, module selectors let you disambiguate which API to use:

import ModuleA
import ModuleB

let x = ModuleA::getValue() // Call 'getValue' from ModuleA
let y = ModuleB::getValue() // Call 'getValue' from ModuleB

Swift 6.3 also enables using the Swift module name to access concurrency and String processing library APIs:

let task = Swift::Task {
  // async work
}

Syntax - String Defaults - Swift 6.2

Nil coalescing for Strings doesn’t work with different types. The following compiles:

var age: Int? = nil
print("Age: \(age ?? 0)")

But this kind of code does not compile:

print("Age: \(age ?? "Unknown")")

Mixing an optional integer with a string default value isn’t allowed. However, this is possible from Swift 6.2 onwards:

print("Age: \(age, default: "Unknown")")

Debugging Logs - Swift 6

Custom LLDB summaries with @DebugDescription

Swift 6 provides a debugging macro to easily customise how an object is displayed in LLDB when using the p command, and in the variables view in Xcode and VSCode, by using a formatting scheme that does not run arbitrary code.

@DebugDescription
struct Organization: CustomDebugStringConvertible {
  var id: String
  var name: String
  var manager: Person
  // ... and more

  var debugDescription: String {
    "#\(id) \(name) [\(manager.name)]"
  }
}

Output:

(lldb) p myOrg
(Organization) myOrg = "`#100 Worldwide Travel [Jonathan Swift]`" 

Inline Arrays - Performance and Stability - Swift 6.2

Swift 6.2 includes features designed to maximize performance without compromising safety. These features help you write safe, low-level code with predictable performance and minimal overhead.

💡 Quick Recap - Heap vs Stack
Imagine the computer’s memory as a giant bedroom.
The Stack is like a neat backpack. It holds your quick, everyday items. It is highly organized, super fast, and ?whatever you put on top must be used first. When you finish a task, those items are thrown away automatically. The catch? It is pretty small and can only hold a few things.The Heap is like a giant toy box. It is massive and can hold all your biggest things, like giant Lego sets. But because it is so big, it can be a bit messy. You have to manually search for exactly what you need, and you have to clean it up when you are done—or the room gets way too cluttered

InlineArray is a new fixed-size array with inline storage for elements, which can be stored on the stack or directly within other types without additional heap allocation.

struct Game {
  // Shorthand for InlineArray<40, Sprite>
  var bricks: [40 of Sprite]

  init(_ brickSprite: Sprite) {
    bricks = .init(repeating: brickSprite)
  }
}

Watch this space, more bitesize Swift goodness to come!


TAGS

SHARE VIA

RELATED POSTS