Concurrency in top-level code
About 3 min
Concurrency in top-level code 관련
HACKING WITH SWIFT
What's new in Swift?
Concurrency in top-level code | Changes in Swift 5.7
Concurrency in top-level code
Available from Swift 5.7
SE-0343 (apple/swift-evolution
) upgrades Swift’s support for top-level code – think main.swift
in a macOS Command Line Tool project – so that it supports concurrency out of the box. This is one of those changes that might seem trivial on the surface, but took (apple/swift
) a lot of work (apple/swift
) to make happen (apple/swift
).
In practice, it means you can write code like this directly into your main.swift
files:
import Foundation
let url = URL(string: "https://hws.dev/readings.json")!
let (data, _) = try await URLSession.shared.data(from: url)
let readings = try JSONDecoder().decode([Double].self, from: data)
print("Found \(readings.count) temperature readings")
Previously, we had to create a new @main
struct that had an asynchronous main()
method, so this new, simpler approach is a big improvement.
Other Changes in Swift 5.7
if let shorthand for unwrapping optionals | Changes in Swift 5.7
if let shorthand for unwrapping optionals
Multi-statement closure type inference | Changes in Swift 5.7
Multi-statement closure type inference
Clock, Instant, and Duration | Changes in Swift 5.7
Clock, Instant, and Duration
Regular expressions | Changes in Swift 5.7
Regular expressions
Type inference from default expressions | Changes in Swift 5.7
Type inference from default expressions
Opaque parameter declarations | Changes in Swift 5.7
Opaque parameter declarations
Structural opaque result types | Changes in Swift 5.7
Structural opaque result types
Unlock existentials for all protocols | Changes in Swift 5.7
Unlock existentials for all protocols
Lightweight same-type requirements for primary associated types | Changes in Swift 5.7
Lightweight same-type requirements for primary associated types
Constrained existential types | Changes in Swift 5.7
Constrained existential types
Distributed actor isolation | Changes in Swift 5.7
Distributed actor isolation
buildPartialBlock for result builders | Changes in Swift 5.7
buildPartialBlock for result builders
Implicitly opened existentials | Changes in Swift 5.7
Implicitly opened existentials
Unavailable from async attribute | Changes in Swift 5.7
Unavailable from async attribute