BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Swift 6.4 Brings Subprocess 1.0, Improved Interoperability, Faster Wasm, and More

Swift 6.4 Brings Subprocess 1.0, Improved Interoperability, Faster Wasm, and More

Listen to this article -  0:00

The latest release of Swift, Swift 6.4, introduces a range of language and tooling improvements, including better performance through expanded support for non-copyable values, up to 40 times faster Wasm generated code, improved interoperability with C++20 and Java, and a new Subprocess library that provides a cross-platform API for launching and interacting with external processes.

The new release extends Swift's syntax to make code simpler and clearer. For example, you can now write AType? instead of (some AType)? or (any AType)?, use the new @diagnose attribute to control compiler diagnostics and warnings, and disambiguate identically named symbols imported from different modules with AModuleName::.

In concurrent code, the defer statement now directly supports the await keyword, eliminating the need to wrap asynchronous calls in a separate Task. Swift 6.4 also introduces withTaskCancellationShield, which temporarily shields a task from cancellation, allowing critical cleanup code to complete even when the task has been cancelled. Swift Concurrency expert Antoine van der Lee, author of a popular Swift blog, noted:

Swift 6.4 brings several practical improvements to Swift Concurrency. [...] If you are migrating a codebase to Swift 6, these updates are worth knowing. Not every feature will change how you write code every day, but together they remove friction from strict concurrency adoption.

Swift 6.4 reduces unnecessary copying and allocations in common array operations with new types and protocols, including UniqueArray, UniqueBox, Ref/MutableRef, and Iterable. [(UniqueBox)] provides a non-reference counted smart pointer that uniquely owns a heap-allocated value and enforces non-copyable semantics. UniqueArray avoids the copy-on-write allocations associated with a regular Array and provides an efficiently growing dynamic buffer. The Iterable protocol enables iteration over elements using borrowing semantics, avoiding unnecessary copies.

Other borrowing-related changes include the new Ref and MutableRef types, which provide first-class, storable containers for borrowing or mutating individual values; borrow and mutate accessors, which enable reading from or modifying a Span or InlineArray through a property; and support for non-copyable types to conform to Equatable, Comparable, and Hashable.

The following snippet show how you declare borrowing and mutating accessors:

struct RigidWrapper<Element: ~Copyable>: ~Copyable {
    var _element: Element
    var element: Element {
        borrow {
            return _element
        }
        mutate {
            return &_element
        }
    }
}

On the IDE front, Swift 6.4 brings more robust debugging support when working with modules, along with a unified build toolchain across all supported platforms, using Swift Build as the default. Speaking of debugging, Hacker News user boxed expressed a sentiment shared by many developers:

I really hope the debugger improvements are noticeable. The number of times I've stopped in a debugger and I can't see the variables I want to look at has been extremely high.

The Subprocess package is now stable, providing a cross-platform API for launching and interacting with external subprocesses. The following snippet demonstrates how to launch a process and capture its output:

let result = try await Subprocess.run(
  .name("ls"),
  arguments: ["-la"],
  output: .string(limit: 4096)
)
print(result.standardOutput)

Swift 6.4 also improves Swift's interoperability with all supported languages. The new @c and @implementation attributes allow you to provide a C header’s implementation directly in Swift, without requiring a separate C declaration. Swift 6.4 transparently bridges C++20's std::span with Swift's Span, and extends interoperability with Java in various way. Finally, Wasm bridging through JavaScriptKit is now up to 40 times faster than dynamic bridging.

Swift 6.4 includes many more changes than can be covered here, so be sure to check the official release announcement for the full detail.

About the Author

Rate this Article

Adoption
Style

BT