Using Call Site Inferencing
Swift’s implicit member expression allows the compiler to automatically infer types on your behalf. You can use implicit expression when arguments are unambiguously of a single type (T or T?) and there’s a static member that returns T. Inferencing tightens enumeration and class method code and makes code easier to read.
For example, in the following code, Swift enables you to drop an explicit UIColor type mention before the .red member. In this sample, the type is fully established by the initializer’s signature. Calls don’t require extra clarification.
| // For this initializer |
| extension UIView { |
| public convenience init(backgroundColor color: UIColor) { |
| self.init(frame: .zero) |
| self.backgroundColor ... |
Get Swift Style now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.