BT

Swift 3.1 Improves Language, Package Manager, and Linux Implementation

| by Sergio De Simone Follow 14 Followers on Mar 31, 2017. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT