BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Apple Open Sources System, Swift Library Interfacing with System-Level API

Apple Open Sources System, Swift Library Interfacing with System-Level API

This item in japanese

System is a low-level library Apple introduced at its last WWDC conference to provide an idiomatic and type-safe interface to system calls and currency types which are usually available at the OS level. In keeping with Apple's aim to push Swift as a cross-platform development ecosystem, System has been open sourced to make it easier for programmers to contribute to its further development across platforms.

System was born as a way to simplify the creation of libraries closer to the OS-layer, such as SwiftNIO and SwiftPM. With the current decision to open source it, Apple realized System can play a much bigger role in fulfilling Swift goal to be an effective tool to create cross-platform programs.

Our vision is for System to eventually act as the single home for low-level system interfaces for all supported Swift platforms.

As mentioned, System attempts to leverage Swift type system to be idiomatic, safe, and expressive. This means, for example, it prefers the use of struct and enum in place of the ubiquitous integers used in the C API, and of strong types, such as FilePath, in place of char*. Moreover, thanks to Swift error handling do/try/catch syntax, it will be harder to ignore errors. Also worth of mention is the fact that all system calls that can be interrupted by a signal default to retryOnInterrupt behaviour, so they will be automatically retried.

This is an example of how System enables opening a file:

let message: String = "Hello, world!" + "\n"
let path: FilePath = "/tmp/log"
let fd = try FileDescriptor.open(
  path, .writeOnly, options: [.append, .create], permissions: .ownerReadWrite)
try fd.closeAfter {
  _ = try fd.writeAll(message.utf8)
}

It is important to remark that System itself does not aim to be cross-platform, meaning it will not expose a common API across all supported platforms. Rather, System aims to adapt its API to resemble what the underlying OS API looks like. This will undoubtedly make it easier for developers to port System to multiple platforms but it also means System will require the use of #if os() guards.

In its actual implementation, the System module only includes a small number of system calls, currency types, and convenience functionality which are tied to the C low-level API that is available on UNIX-like OSes, such as Linux and macOS. Apple says, though, making System available on Windows comes next on its roadmap, which somehow makes a stronger case for the recent announcement of Swift for Windows.

You can add System to your Swift project using the Swift Package Manager, where you can refer to it as "SystemPackage", like in the following example:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        .package(url: "https://github.com/apple/swift-system", from: "0.0.1"),
        // other dependencies
    ],
    targets: [
        .target(name: "MyTarget", dependencies: [
            .product(name: "SystemPackage", package: "swift-system"),
        ]),
        // other targets
    ]
)

A note of caution is due here since System is in its early stages and bound to change a lot, so you can expect compatibility only across minor version, e.g., between 0.0.1 and 0.0.3.

Rate this Article

Adoption
Style

BT