[swift-evolution] Request for Discussion: Setup closures
Adrian Zubarev
adrian.zubarev at devandartist.com
Sun Dec 6 05:59:16 CST 2015
I come across this topic and I instantly was overwhelmed by this idea of the Setup Closures. I build a little extension just to test it with UIKit. I thought adding this keyword for Setup Closures was a good idea. If we also could remove this in from the closure it would look good on my opinion.
protocol SettableType {
init() // designated initializer
}
extension SettableType {
init(@noescape setup: (this: Self) -> Void) {
self.init()
setup(this: self)
}
}
extension UIView: SettableType {}
let view = UIView()
let questionLabel = UILabel() { this in /// <- REMOVE
this.textAlignment = .Center
this.font = UIFont(name:"SomeFontName", size: 72)
this.text = "Hello World"
this.numberOfLines = 0
view.addSubview(this)
}
—
Regards Adrian
Am 6. Dezember 2015 bei 01:16:31, ilya via swift-evolution (swift-evolution at swift.org) schrieb:
> PROBLEM: With many Apple-supplied classes, typical initializers fail to fully set up an instance for use. Here's one example: ...
FWIW, I created a configuration operator more then a year ago, and use it in all of my Swift projects:
let task = NSTask() +=+ {
$0.launchPath = "/usr/bin/mdfind"
$0.arguments = ["kMDItemDisplayName == *.playground"]
$0.standardOutput = pipe
}
Note you can also use the configured object in the rhs:
let questionLabel = UILabel() +=+ {
$0.textAlignment = .Center
$0.font = UIFont(name:"DnealianManuscript", size: 72)
$0.text = currentQuestion.questionText
$0.numberOfLines = 0
view.addSubview($0)
}
This $0. certainly looks ugly and it would be great to be able to simplify this. I don't llike the following much though (dot-syntax can be ambiguos here, and using simply a method name is even worse):
let questionLabel = UILabel() +=+ {
.textAlignment = .Center
.font = UIFont(name:"DnealianManuscript", size: 72)
.text = currentQuestion.questionText
.numberOfLines = 0
view.addSubview($0)
}
Actually I would be happy with something like
let questionLabel = UILabel() .{
..textAlignment = .Center
..font = UIFont(name:"DnealianManuscript", size: 72)
..text = currentQuestion.questionText
..numberOfLines = 0
view.addSubview($0)
}
Other thoughts?
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/8af1d99d/attachment.html>
More information about the swift-evolution
mailing list