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, 1st Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.