fupio

Fupio is the easiest way to share online Learn more »

Join Fupio
Hacking with Swift

Hacking with Swift

This year's spring release of Swift brings with it a number of welcome additions and refinements that will make small but important improvements to many projects.
Things like support for trailing commas in lists have been on many people's wish lists for some time, and the extended use of nonisolated helps work around one of the pain points folks encounter with Swift concurrency, but ultimately these changes are mostly minor and provide a neat run up to larger changes that will come in Swift 6.2.
Let's take a look at what's changing…

Allow trailing comma in comma-separated lists

SE-0439 adjusts Swift's syntax so that trailing commas are now permitted in arrays, dictionaries, tuples, function calls, generic parameters, string interpolation, and indeed anywhere a list of items is bound by parentheses (), brackets [], or angle brackets <>.
So, this kind of code is now allowed:
func add<T: Numeric,>(_ a: T, _ b: T,) -> T {
    a + b
}

let result = add(1, 5,)
print(result,)
In practice you're unlikely to write that kind of code, but this feature is really useful in code like this:
import Foundation

let message = "Reject common sense to make the impossible possible."

let range = message.range(
    of: "impossible",
    options: .caseInsensitive
)
You can see the call to range(of:options) is split across multiple lines, which is a fairly common coding style. It looks for the string "impossible" in a case-insensitive search, but thanks to the support for trailing commas we can comment out the whole options: .caseInsensitive line entirely without breaking our code – the line before ends with a trailing comma, but that's allowed from Swift 6.1 onwards.
T...
https://www.hackingwithswift.com/articles/276/whats-new-in-swift-6-1

Comments