Today, Apple open sourced the Swift programming language as promised in WWDC 2015. The code is available under Apple's newly created GitHub repository, under the Apache license, with the source code split into several different repositories:
- The Swift compiler and core libraries, along with SourceKit (which is used to provide information to IDEs) with history going back to July 2010
- A set of evolution documents which shows how Swift will evolve in the future, including future plans for Swift 3.0 and a stable binary API
- The Swift Foundation library (
import Foundation
), which maps to the Foundation framework used by Objective-C applications - The Swift libdispatch library (
import Dispatch
), which provides functionality related to GCD in Swift - The XCTest core libraries (import XCTest), which provides a hook for testing Swift based applications
There are also two separate packages available; the Swift package manager and the Swift build tools which can be used to build the Swift compiler and environment. There's more information about the package manager along with a community proposal for a new package manager (which shows how hard Apple is working at making this a successfully open source project). The PackageDescription
module introduces a new type, Package
, which represents metadata about a version and a name, as well as a list of dependencies. The implementation appears to be influenced with NPM, in that the dependencies are represented as source dependencies to remote Git repositories; when the build is executed, those dependencies are checked out, compiled to statically linked libraries and then included in the application. The package is kicked off by running a new swift build
command, which downloads any dependencies defined in the Package.swift
file. Dependencies are versioned using semantic versioning (which may be enforced in future), although it's not yet clear whether exact package versions can be specified or only a single version. The example given in the documentation shows:
import PackageDescription
let package = Package(
name: "DeckOfPlayingCards",
targets: [],
dependencies: [
.Package(url: "https://github.com/apple/example-package-fisheryates.git",
majorVersion: 1),
.Package(url: "https://github.com/apple/example-package-playingcard.git",
majorVersion: 1),
]
)
Since Swift is built upon tools like LLVM and Clang, there are forks of swift-llvm, swift-clang, swift-lldb and swift-cmark which is used to generate documentation for the Swift source files. Unlike their SVN counterparts, these are hosted in Git and will be much easier to maintain in the future.
Swift aims to build a community by accepting contributions. A new blog provides information about Swift development, which may ultimately supersede the blog at Apple's main website. There is a community code of conduct (which is based on the one defined at contributor-covenant.org) as well as a number of mailing lists including swift-dev and swift-corelibs-dev. A number of pull requests have been merged already despite the project only having been publicly available for a few hours.
Because Swift libraries are copied into executables, the Apache License has a runtime library exception added which permits the application to be used without providing attribution:
As an exception, if you use this Software to compile your source code and portions of this Software are embedded into the binary product as a result, you may redistribute such product without providing attribution as would otherwise be required by Sections 4(a), 4(b) and 4(d) of the License.
This text is added at the end of the standard Swift license, which means that although it is based on the standard Apache License there may be some considerations necessary for legal re-approval. The idea of runtime exceptions is not new; for example, Oracle licenses Java under the GPL license but with a classpath exception that allows code to be linked without using the full GPL.
Finally, one of the main questions about Apple's open-sourcing of Swift is: would it run on other operating systems? The answer is yes; there is a Linux port and there are pre-built binaries for Ubuntu available from the Swift.org download page, although it isn't quite as full-featured as the OSX port.
Swift on Linux does not depend on the Objective-C runtime nor includes it. While Swift was designed to interoperate closely with Objective-C when it is present, it was also designed to work in environments where the Objective-C runtime does not exist.
Fortunately, you can run and debug Swift applications on Linux using the REPL so it's possible to get started with Linux and Swift easily and quickly. There is some missing behaviour; for example, those things that require the Objective-C bridge (such as some String APIs that use NSString's implementation under the covers, or classes marked with @objc
) will not work on the Linux port at the moment. There are also some disconnects between general C APIs such as variable args support and limited libdispatch functionality, which is being worked on the GitHub project.
Will you be interested in learning Swift now that it's available outside of the Apple ecosystem?