Here is a real world example of where I use defaulted parameters: public extension UIView { public convenience init( _ w: CGFloat, _ h: CGFloat, position: CGPoint = .zero, backgroundColor: UIColor = UIColor.whiteColor(), translucency alpha: CGFloat = 1.0, borderWidth: CGFloat = 0.0, borderColor: UIColor = UIColor.blackColor(), cornerRadius: CGFloat = 0.0 ){ self.init(frame: CGRect(x: position.x, y: position.y, width: w, height: h)) self.backgroundColor = backgroundColor.colorWithAlphaComponent(alpha) self.layer.borderWidth = borderWidth self.layer.borderColor = borderColor.CGColor self.layer.cornerRadius = cornerRadius self.translatesAutoresizingMaskIntoConstraints = false } } It's not a beautiful initializer or aspirational code. It is convenient for putting together flexible views in playgrounds. This utility controls the axes I typically want to tweak for view creation: * How big is it (w, h) * Where is it? (position, if used, for example, as a subview) * What color is it? Is the color solid or translucent? (backgroundColor, translucency) * Does it have a border? and if so, how wide and what color? (borderWidth, borderColor) * Are the corners rounded and to what degree? (cornerRadius) The reason I present this example is that while there are relationships between some of the defaulted values, there is absolutely no inherent order to those groups. When putting together a view, I may later decide to add, for example, the border. Should I have to know that the border must appear before any background color settings? Having to figure out where a previously defaulted value should be inserted when adding an explicit setting imposes an undue burden on the programmer, reduces flexibility during experimentation, and removes one of the neatest Swift features. For this reason, I vote no on the proposal. By rejecting this proposal, Swift retains a flexibility that defers to the programmer, placing the programmer's needs before any benefits that may accrue to the compiler. I have followed the discussion. I believe it goes against the general Swift philosophy. And while the change being proposed is significant to be part of a formal language review process, I think adopting it would be the wrong thing to do. -- Erica
|
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
