Multi-statement closure type inference
About 2 min
Multi-statement closure type inference 관련
HACKING WITH SWIFT
What's new in Swift?
Multi-statement closure type inference | Changes in Swift 5.7
Multi-statement closure type inference
Available from Swift 5.7
SE-0326 (apple/swift-evolution
) dramatically improves Swift’s ability to use parameter and type inference for closures, meaning that many places where we had to specify explicit input and output types can now be removed.
Previously Swift really struggled for any closures that weren’t trivial, but from Swift 5.7 onwards we can now write code like this:
let scores = [100, 80, 85]
let results = scores.map { score in
if score >= 85 {
return "\(score)%: Pass"
} else {
return "\(score)%: Fail"
}
}
Prior to Swift 5.7, we needed to specify the return type explicitly, like this:
let oldResults = scores.map { score -> String in
if score >= 85 {
return "\(score)%: Pass"
} else {
return "\(score)%: Fail"
}
}
Other Changes in Swift 5.7
if let shorthand for unwrapping optionals | Changes in Swift 5.7
if let shorthand for unwrapping optionals
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
Concurrency in top-level code | Changes in Swift 5.7
Concurrency in top-level code
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