Related implicit expressions in Swift 5.4

In Swift 5.4: Implicit expressions for class members (also known as "dot syntax") can now be used even when accessing a property or method as a result of such an expression, as long as the final return type remains the same.





Please note that at the time of this writing, Swift 5.4 is in beta testing as part of Xcode 12.5 .





In practice, this means that whenever we create an object or value using a static API, or when accessing an enumerated type, we can now directly call a method or property on that class instance, and the compiler can still infer that type. to which we are referring.





For example, when instantiating a UIColor using one of the built-in static APIs provided as part of the system, we can now easily change the alpha component of that color without having to explicitly reference the UIColor itself in situations such as:





// In Swift 5.3 and earlier, an explicit type reference is always
// required when dealing with chained expressions:
let view = UIView()
view.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
...

// In Swift 5.4, the type of our expression can now be inferred:
let view = UIView()
view.backgroundColor = .blue.withAlphaComponent(0.5)
...
      
      



, API, , UIColor, :





extension UIColor {
    static var chiliRed: UIColor {
        UIColor(red: 0.89, green: 0.24, blue: 0.16, alpha: 1)
    }
}

let view = UIView()
view.backgroundColor = .chiliRed.withAlphaComponent(0.5)
...
      
      



, , API. , ยซ API Swiftยป API, , , :





extension ImageFilter {
    static var dramatic: Self {
        ImageFilter(
            name: "Dramatic",
            icon: .drama,
            transforms: [
                .portrait(withZoomMultipler: 2.1),
                .contrastBoost,
                .grayScale(withBrightness: .dark)
            ]
        )
    }
}
      
      



Swift 5.4 ( ) - , ImageFilter, .transforms:





extension ImageFilter {
    func combined(with filter: Self) -> Self {
        var newFilter = self
        newFilter.transforms += filter.transforms
        return newFilter
    }
}
      
      



, . , .





let filtered = image.withFilter(.dramatic.combined(with: .invert))
      
      



! , API- , , , .








All Articles