[swift-evolution] Customized Inline Init Closure

Weston Catron wcatron at catrondevelopment.com
Tue Aug 30 08:44:28 CDT 2016


Jo-

Sorry for such a delayed response. $0 is interesting, is this a common language concept elsewhere? I would still push for using the variable name or possibly a new keyword (this, self2, it), which is really just a more expressive $0. Or don’t keep self and just stick with the limitation of having to pass through any necessary information in the scope. In javascript it is common, albeit redundant/ugly, to do something like:

```
var that = this; 
$(…).each(function() { 
    this.foo(that) 
});
```

$0 is sort of an implied ` let $0 = self; ` before each closure.

Alternatively although this is sort of reverting back to a more verbose style we could specifically give our scope.

```
let me = person (name: “Weston”, dob: ..., that: self) {
    self.name = name;
    self.age = that.getAgeSinceDOB(dob);
    self.gender = that.calculateGender();
}
```
However I do not like this.

What do you think about a key word to replace $0 that is more expressive and easy to understand for new programmers? Or do you think recasting self only when necessary would be logical?

Weston


> Here is another option. Attached is a playground I was messing around with. There are some weird bugs I was noticing, but don’t quite know if they are important enough to submit (comment out line 54 to see).
> 
> I actually like using the $0 so as to allow access to self if using within another type (ex: view controller code below).
> 
> Please respond with any potential issues with the code I have written.
> 
> - Jo
> 
> 
> 
> protocol ClosureInit { init() }
> 
> extension ClosureInit {
> 
> init(@noescape b: inout Self ->Void) { self.init(); b(&self) }
> 
> }
> 
> struct Person: ClosureInit {
> 
> enum GenderType: String { case Male, Female }
> 
> var age: Int = 0
> var gender: GenderType?
> var name: String?
> 
> }
> 
> let me = Person {
> 
> $0.name = "Jo"
> $0.age = 32
> $0.gender = .Male
> 
> }
> 
> me.age // 32
> 
> extension Array: ClosureInit { }
> 
> let randomIntArray = [Int] {
> 
> for _ in 0...10 {
> 
> $0.append(Int(arc4random_uniform(100)))
> 
> }
> 
> }
> 
> randomIntArray
> 
> let personArray = [Person] {
> 
> for _ in 0...8 {
> 
> $0.append(Person {
> 
> $0.age = Int(arc4random_uniform(100))
> $0.gender = Int(arc4random_uniform(100)) % 2 == 0 ? .Male : .Female // comment this line out to see error
> 
> 
> })
> 
> }
> 
> }
> 
> personArray
> 
> extension UIView: ClosureInit { }
> 
> class ViewController: UIViewController {
> 
> override func viewDidLoad() {
> super.viewDidLoad()
> 
> UILabel {
> 
> $0.text = "This is Awesome!"
> $0.textColor = UIColor.cyanColor()
> $0.frame = CGRect(x: 20, y: 20, width: view.frame.width - 40, height: 40)
> view.addSubview($0)
> 
> }
> 
> view.addSubview(UIButton {
> 
> $0.setTitle("Submit", forState: .Normal)
> $0.frame = CGRect(x: 20, y: 60, width: view.frame.width - 40, height: 40)
> 
> })
> 
> 
> }
> 
> }
> 
> let vc = ViewController()
> 
> vc.loadViewIfNeeded()
> 
> vc.view.subviews
> 
> 
> 
> 
> 
> 
> 


More information about the swift-evolution mailing list