<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I agree with the intent of this, but perhaps not the solution; sticking with the original example, simply being able to have an implied initialiser for classes, as we have for structs, would be enough, for example:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Monaco" class="">class Foo {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo:String</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let bar:String</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let baz:Int</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var barCount:Int { return self.bar.characters.count } // Computed property, could be lazy instead I think?</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// Implied init(foo:String, bar:String, baz:Int)</font></div><div class=""><font face="Monaco" class="">}</font></div></blockquote><div class=""><br class=""></div><div class="">Personally I’m not a fan of the syntax, I wonder if an #autoinit or such directive could be used to indicate properties that should be initialised automatically where possible, like so:</div><div class=""><div class=""><font face="Monaco" class=""><br class=""></font></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class=""><font face="Monaco" class="">class Foo {</font></div></div><div class=""><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>#autoInit let foo:String</font></div></div><div class=""><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>#autoInit let bar:String</font></div></div><div class=""><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>#autoInit let baz:Int</font></div></div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let barCount:Int</div><div class=""><div class=""><font face="Monaco" class=""><br class=""></font></div></div><div class=""><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>init(foo:String, bar:String, baz:Int) {</font></div></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>// foo, bar, baz handled behind the scenes</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>self.barCount = bar.characters.count</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><div class=""><font face="Monaco" class="">}</font></div></div></blockquote><div class=""><br class=""></div><div class="">(or it could be on the parameters of the init function, might be clearer that way)</div><div class=""><br class=""></div><div class="">Just an idea anyway</div><br class=""><div><blockquote type="cite" class=""><div class="">On 16 Apr 2016, at 14:17, Jonathan Hull via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Since we know the types of the properties, how about we replace the type in the signature with either an indication that the property should be automatically set, or better yet, the property which should be set:</div><div class=""><br class=""></div><div class="">class Foo</div><div class="">{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let foo : String</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let bar : String</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let barCount : Int</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let baz : Int</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init(foo: self.foo, bar: self.bar, baz: self.baz)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>self.barCount = bar.characters.count</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>} </div><div class="">}</div><div class=""><br class=""></div><div class="">That way you don’t always have to have the init’s parameter names match the names of the properties they set (even though they often would). We could also allow a leading dot as a shorthand for ‘self.’</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init(foo: .foo, bar: .bar, baz: .baz)</div><div class=""> </div><div class="">I think I like explicit ‘self.’ better, but that may just be my preference. In either case, the generated interface would show the actual type.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// init(foo: String, bar: String, baz: Int)</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><pre style="white-space: pre-wrap; background-color: rgb(255, 255, 255);" class="">This is a common pattern for initialisers at the moment:
class Foo
{
let foo : String
let bar : String
let barCount : Int
let baz : Int
init(foo: String, bar: String, baz: Int)
{
self.foo = foo
self.bar = bar
self.baz = baz
barCount = bar.characters.count
}
}
This involves a lot of using 'self.'. For those who prefer not to use
'self.' explicitly everywhere, this is probably the main place it gets
used. It's a lot of boilerplate code.
How would it be if, like default variables, we could pack some of that
information into the argument tuple, and unify parameters with properties
immediately?
class Foo
{
let foo : String
let bar : String
let barCount : Int
let baz : Int
init(self.foo: String, self.bar: String, self.baz: Int)
{
barCount = bar.characters.count
}
}
Less boilerplate, more focus on the properties which need to be generated.
Thoughts?</pre></blockquote><div class=""><br class=""></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>