[swift-evolution] What about a VBA style with Statement?

Milos Rankovic milos at milos-and-slavica.net
Thu Apr 14 05:41:10 CDT 2016


> On 13 Apr 2016, at 17:04, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The biggest advantage of the with pattern IMO is Cocoa initializers. It provides a more unified 
> initialization scope. Instead of:
> 
> let questionLabel = UILabel()
> questionLabel.textAlignment = .Center
> questionLabel.font =  UIFont(name:"DnealianManuscript", size: 72)
> questionLabel.text = currentQuestion.text
> questionLabel.numberOfLines = 0

Whilst I would not mind the `with` pattern, it’s worth noting that Swift already has some very fine alternatives:

	let questionLabel: UILabel = {
		let $ = UILabel()
		$.textAlignment = .Center
		$.font = UIFont(name:"DnealianManuscript", size: 72)
		$.text = "?"
		$.numberOfLines = 0
		return $
	}()

Including some lazy options:
	
	private(set) lazy var questionLabel: UILabel = { ... }()
	
	static let questionLabel: UILabel = { ... }()

milos

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160414/8814c88f/attachment.html>


More information about the swift-evolution mailing list