The Swift programming language team has announced its library for idiomatic interfaces is now open source. Swift System was first introduced in June for Apple platforms. It provides idiomatic interfaces to system calls and low-level types. As part of the announcement, it now includes Linux support.
“Most operating systems today support some flavor of system interfaces written in C that have existed for decades. While it is possible to use these APIs directly from Swift, these weakly-typed system interfaces imported from C can be error-prone and unwieldy,” Michael Ilseman, a engineer on the Swift Standard library team at Apple, wrote in a blog post.
The weakly-typed functions fail to utilize the expressivity and type safety of Swift because the semantic rules aren’t captured in the API’s signature, preventing the programming language from guiding the user towards correct usage of the API, according to Ilseman.
Meanwhile, the ‘System’ module utilizes these various language features to bear expressivity and eliminate many opportunities for error.
For example, ‘System’ defines the open system call as a static function with default arguments in the FileDescriptor namespace.
Mainly, System pervasively uses raw representable structs and option sets and the strong types help catch mistakes at compile time. Also, errors are thrown using the standard language mechanism and cannot be missed.
Last but not least, ‘FilePath’ is a managed, null-terminated bag-of-bytes that conforms to ‘ExpressibleByStringLiteral’ — far safer to work with than a ‘UnsafePointer<CChar>.’
Swift’s immediate goal is to simplify building cross-platform libraries and applications such as SwiftNIO and the Swift Package Manager. This will include enhancements to FilePath and adding support for the recently announced Swift on Windows.