With WWDC coming up in just two and a half weeks time, there is still a lot going on when it comes to Swift.

Podcasts

Jesse and JP discuss “Removing Things From Swift”, talking about implicit returns and eliding commas.

News and community

Zhuowei Zhang and Max Desiatov worked on initial support for compiling Swift to WebAssembly. 😱

Commits and pull requests

Raj Barik merged a pull request with an entire optimizer pass to convert existentials to generic parameters, creating new opportunities for unboxing and devirtualization.

Returned proposals

SE-0258: Property Delegates was returned for revision.

There was a lot of great discussion in this review, leading to some very useful feedback for the Core Team and the proposal authors. That feedback can be broken down into two categories: (1) procedural feedback about the state of the proposal and its review and (2) technical feedback about the actual proposal. Both are useful, and we’ll consider each in turn.

You can read the full post.

Proposals in review

SE-0260: Library Evolution for Stable ABIs is under review.

One of Swift’s goals is to be a good language for libraries with binary compatibility concerns, such as those shipped as part of Apple’s OSs. This includes giving library authors the flexibility to add to their public interface, and to change implementation details, without breaking binary compatibility. At the same time, it’s important that library authors be able to opt out of this flexibility in favor of performance.

SE-0253: Callable values of user-defined nominal types is under review.

This proposal introduces “statically” callable values to Swift. Callable values are values that define function-like behavior and can be called using function call syntax. In contrast to dynamically callable values introduced in SE-0216, this feature supports statically declared arities, argument labels, and parameter types, and is not constrained to primary type declarations.

In a nutshell, values that have a method whose base name is call (referred to as a “call method” for the rest of this proposal) can be called like a function. The function call syntax forwards arguments to the corresponding call method.

struct Adder {
    var base: Int
    func call(_ x: Int) -> Int {
        return base + x
    }
}

let add3 = Adder(base: 3)
add3(10) // => 13

Swift Forums

Michael Ilseman shared a case study omitting returns in String.

I’m a fan of Nate Chandler’s SE-0255 which allows us to omit the return keyword in a single-expression function or computed variables. Here is a case study of SE-0255 as applied to String’s internal implementation.

Ding Ye pitched a proposal to add isPowerOf2 to BinaryInteger.

Checking some mathematical properties of integers (e.g., parity, divisibility, etc.) is widely used in scientific and engineering applications. While Swift’s standard library provides a convenient method isMultiple(of:) to test whether an integer is a multiple of another, there are other cases that are not yet supported. A frequently used one is to check if an integer is power of two. This pitch would like to address this problem by adding a computed property isPowerOf2 to the BinaryInteger protocol.

Giuseppe Lanza pitched a proposal to make sorting algorithms selectable.

It’s currently unclear without peeking under the hood of swift implementation is the algorithm used for sorting, changes to this algorithm (recently it passed from introsort to timsort) and performances tradeoffs.

Different sorting algorithms might be preferred depending on the problem that needs to be solved. In certain cases it’s important to know that the sorting is stable, in other cases might be important to be sure that the algorithm is in place.

Igor Kulman pitched a proposal to add a sum-method to numeric arrays.

I currently sum the arrays using .reduce(0,+) which I do not mind by the code is not that readable

let total = expenses.map({ $0.amount }).reduce(0,+)

Another bigger problem than readability is that I encounter code like this from other developers all the time:

var sum = 0
for expense in expenses {
    sum = sum + expense.amount
}

This code might come from imperative thinking but it might also come from not knowing how to use .reduce() which can look like an intimidating method. A .sum() would be friendlier and much easier to use even to inexperienced developers.

Philipp Heller pitched a proposal to allow Protocol Conformance for Tuples and Anonymous Structs.

Currently, if you wish to pass some data to a function that requires the data to conform to a specific protocol, you have to define some type that conforms to the protocol and create an object with that type afterward.

Right now tuples cannot be used as literals to construct structs. Assume there is a function prettyPrintAsJson(data: Encodable) that you only need to call once in your code. You would define a struct (or class) first and create an instance of that type afterward.

Finally

Still going 💪 (puns are the best)