Structural opaque result types
About 2 min
Structural opaque result types 관련
HACKING WITH SWIFT
What's new in Swift?
Structural opaque result types | Changes in Swift 5.7
Structural opaque result types
Available from Swift 5.7
SE-0328 (apple/swift-evolution
) widens the range of places that opaque result types can be used.
For example, we can now return more than one opaque type at a time:
import SwiftUI
func showUserDetails() -> (some Equatable, some Equatable) {
(Text("Username"), Text("@twostraws"))
}
We can also return opaque types:
func createUser() -> [some View] {
let usernames = ["@frankefoster", "@mikaela__caron", "@museumshuffle"]
return usernames.map(Text.init)
}
Or even send back a function that itself returns an opaque type when called:
func createDiceRoll() -> () -> some View {
return {
let diceRoll = Int.random(in: 1...6)
return Text(String(diceRoll))
}
}
So, this is another great example of Swift harmonizing the language to make things consistently possible.
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
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
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