Swift System Is Now Open Source With Linux Support
Back in June this year, Apple engineers introduced Swift System — a new library to interface with low-level currency types and system calls. Today, Apple open-sourced Swift System and also added Linux support.
The idea behind Swift System is to present a one-stop solution to low-level system interfaces for all supported Swift platforms. So let’s check out what’s new in it:
Leaving Imported C Interfaces Behind
The majority of the operating systems we use today work on support some type of system interface written in C and it has remained the same for decades. These APIs can be used directly from Swift, but such weakly-typed system interfaces imported from C are prone to error and are unmanageable.
For instance, in UNIX-like operating systems such as Linux and Apple’s OS, weakly-typed functions have several cons and they fail to utilize the expressivity and type safety one can find in Swift.
Also Read: Swift Programming Language Now Available On Windows 10
Introducing Idiomatic Swift Interfaces
The Swift System module introduces several language features to improve expressivity and reduces the chances error. For e.g. System
defines the open system call as a static function with defaulted arguments in the FileDescriptor namespace:
On comparing this version to the original version in C, you can spot significant differences such as:
System
uses raw representable structs and option sets. Such strong types help in identifying mistakes at compile time and are easier to convert to and from the weaker C types.- Errors are thrown using the standard language mechanism and cannot be missed.
FilePath
is a managed, null-terminated bag-of-bytes that conforms toExpressibleByStringLiteral
— far safer to work with than aUnsafePointer<CChar>
.
It is important to note that System
is not a cross-platform but a multi-platform library. It provides a separate set of APIs and behaviors on every supported platform, which closely reflects the underlying OS interfaces. Therefore, a single import
will pull in the native platform interfaces specific for the OS you choose.