BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Swift 3.1 Improves Language, Package Manager, and Linux Implementation

Swift 3.1 Improves Language, Package Manager, and Linux Implementation

Bookmarks

Staying true to its plan, the recently announced Swift 3.1 is source compatible with Swift 3.0. Still, it includes a number of changes to the language, the standard library, and improved Linux implementation.

On the language front, two new members have been added to the Sequence protocol: drop(while:) and prefix(while:). They return the subsequence obtained by dropping or including the initial elements of a sequence while a given predicate is true. So, if you have a sequence s, you can take its n-th to m-th subsequence by executing:

let subseq = s.prefix(while: {$0 < m}).drop(while: {$0 < n})

Swift 3.1 also adds a number of conversion initializers for all numeric types, including Int, Float, and Double types, which either produce a correct result, or return nil. Failable initializers, as they are called, are meant to better address conversion from loosely typed data, such as those represented in a JSON format. All numeric types have thus a new initializer that uses the exactly keyword, e.g.,

init?(exactly value: Int64)

Having initializers that can fail has been preferred to initializers that can throw an exception based on community feedback.

Not mentioned in the official announcement, but documented in the updated Apple’s guide to Swift 3.1, type extension can now include a generic where clause:

extension Container where Item == Double {
    func average() -> Double {
        var sum = 0.0
        for index in 0..<count {
            sum += self[index]
        }
        return sum / Double(count)
    }
}

Previously, the where clause could only be used to constrain a generic to a given protocol, which could lead to introducing ad-hoc protocol only used to specify that constraint:

extension Container where Item: MyConstrainProtocol {
...

As a final note about the language, Swift 3.1 extends the @available attribute so it allows to express availability by Swift version, whereas it previously only allowed to specify availability by language and platform:

@available(swift, introduced: 3.0, obsoleted: 3.1)
class Foo {
  //...
}

This relieves programmers from using conditional compilation to juggle with alternate versions of functions, declarations, etc.

The Linux implementation of Swift has seen improvements to many existing classes, including NSDecimal, URLSession, NSArray, NSData, improved JSON serialization performance, and many others.

Finally, the Swift Package Manager has got a number of awaited features, such as:

  • editable packages, which allows to make a package editable using the swift package edit command. This means the package is moved under the user’s Package directory and exempted from dependencies updates, so the user is free to commit and push changes to it.
  • version pinning, using the swift package pin and swift package unpin commands, or editing the Package.pins file.
  • version requirement, which allows a package to adopt new Swift features without breaking clients that use an older toolchain. Package versions that require newer features are automatically ignored when updating packages on an older toolchain.
  • Swift compatibility version, which is similar in spirit to the previous features but regarding the language version, either 3 or 4, used for a package.

Swift 3.1 is available through Xcode 8.3 on macOS, a binary distribution for Ubuntu, and as source code for other platforms from GitHub.

Rate this Article

Adoption
Style

BT