Improved implicit member syntax
About 2 min
Improved implicit member syntax 관련
HACKING WITH SWIFT
What's new in Swift?
Improved implicit member syntax | Changes in Swift 5.4
Improved implicit member syntax
Available from Swift 5.4
SE-0287 (apple/swift-evolution
) improves Swift’s ability to use implicit member expressions, so rather than just having support for exactly one single static member you can make chains of them.
Swift has always had the ability to use implicit member syntax for simple expressions, for example if you wanted to color some text in SwiftUI you could use .red
rather than Color.red
:
import SwiftUI
struct ContentView1: View {
var body: some View {
Text("You're not my supervisor!")
.foregroundColor(.red)
}
}
Prior to Swift 5.4 this did not work with more complex expressions. For example, if you wanted your red color to be slightly transparent you would need to write this:
struct ContentView2: View {
var body: some View {
Text("You're not my supervisor!")
.foregroundColor(Color.red.opacity(0.5))
}
}
From Swift 5.4 onwards the compiler is able to understand multiple chained members, meaning that the Color
type can be inferred:
struct ContentView3: View {
var body: some View {
Text("You're not my supervisor!")
.foregroundColor(.red.opacity(0.5))
}
}
Other Changes in Swift 5.4
Multiple variadic parameters in functions | Changes in Swift 5.4
Multiple variadic parameters in functions
Local functions now support overloading | Changes in Swift 5.4
Local functions now support overloading
Creating variables that call a function of the same name | Changes in Swift 5.4
Creating variables that call a function of the same name
Result builders | Changes in Swift 5.4
Result builders
Property wrappers are now supported for local variables | Changes in Swift 5.4
Property wrappers are now supported for local variables
Packages can now declare executable targets | Changes in Swift 5.4
Packages can now declare executable targets