<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="">On the one hand, I now see the naivety of my example, but I want to emphasize the <u class="">idea</u> behind it, and possibly behind some your proposals Matthew, and that is: <i class="">how can we reduce code duplication and make code more maintainable?</i>. Managing the proliferation of state in programs causes the majority of our headaches. If Swift can help mitigate this, we should put our thinking caps on and figure out a way.<div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On Jan 11, 2016, at 10:40 PM, Matthew Johnson <<a href="mailto:matthew@anandabits.com" class="">matthew@anandabits.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class=""><br class="Apple-interchange-newline">On Jan 11, 2016, at 1:58 PM, David James 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=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">NOTE: this may cross-over with other proposals. I’m totally fine with converging, but just want this idea vetted first.</div></div></blockquote><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi David. Did have you seen the Parameter Forwarding proposal I sent out last night? That would handle this use case, among others. Here’s a link to the proposal (there is also a mailing list thread): <a href="https://github.com/anandabits/swift-evolution/blob/parameter-forwarding/proposals/NNNN-parameter-forwarding.md" class="">https://github.com/anandabits/swift-evolution/blob/parameter-forwarding/proposals/NNNN-parameter-forwarding.md</a></div><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Thanks for providing a concrete example of how that proposal can be used!</div><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Matthew</div><br class="" style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><blockquote type="cite" class="" style="font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">The idea is to reduce code duplication and improve maintainability, particularly around the constructing of objects. Objects hold fields which are subject to schema change, and therefore must be maintainable. The goal with this proposal/idea is to avoid replicating field names throughout the code, particularly when objects are being constructed.</div><div class=""><br class=""></div><div class="">One example is with the Builder pattern, which separates configuration of an object from it’s creation.</div><div class=""><br class=""></div><div class="">Here is some sample code. I have made<span class="Apple-converted-space"> </span><b class="">bold</b><span class="Apple-converted-space"> </span>the suggested changes.</div><div class=""><br class=""></div><div class="">Models:</div><div class=""><br class=""></div><div class=""><font face="Courier" class="">struct Profile {<br class=""> var user:Person?<br class=""> var shippingAddress:Location?<br class=""> var billingAddress:Location?<br class="">}<br class=""><br class="">struct Person {<br class=""> let givenName:String<br class=""> let familyName:String<br class="">}<br class=""><br class="">struct Location {<br class=""> let street:String<br class=""> let town:String<br class=""> let region:String<br class=""> let country:String<br class=""> init <b class="">custom</b><span class="Apple-converted-space"> </span>(street: String, town: String, region: String, country: String) {<br class=""> // ^^ initializer with label. contrived example.<br class=""> self.street = street<br class=""> self.town = town<br class=""> self.region = region<br class=""> self.country = country<br class=""> }<br class="">}<br class=""></font><br class="">Here is a typical builder. Notice the field name<span class="Apple-converted-space"> </span><u class="">duplication</u>, in the method signatures, bodies, and the model objects above. This is a code smell. Granted, it can be resolved in other ways, but we tend to see this kind of duplication everywhere surrounding the model layer, and it causes maintenance problems.</div><div class=""><br class=""><font face="Courier" class="">struct TypicalBuilder {<br class=""> var profile:Profile = Profile()<br class=""> mutating func setUser(givenName: String, familyName: String) {<br class=""> profile.user = Person(givenName: givenName, familyName: familyName)<br class=""> }<br class=""> mutating func setShippingAddress(street: String, town: String, region: String, country: String) {<br class=""> profile.shippingAddress = Location(street: street, town: town, region: region, country: country)<br class=""> }<br class=""> mutating func setBillingAddress(street: String, town: String, region: String, country: String) {<br class=""> profile.billingAddress = Location(street: street, town: town, region: region, country: country)<br class=""> }<br class=""> func build() -> Profile {<br class=""> return profile<br class=""> }<br class="">}<br class=""></font><br class="">Here’s my idea for <i class="">initializer forwarding</i>. </div><div class=""><br class=""><font face="Courier" class="">struct BetterBuilder {<br class=""> var profile:Profile = Profile()<br class=""> mutating func setUser(<b class="">forward: Person.init</b>) {<br class=""> // .init without label would be equivalent to memberwise initializer<br class=""> // "forward” could be an arbitrary label<br class=""> profile.user = Person(<b class="">forward</b>)<br class=""> }<br class=""> mutating func setShippingAddress(<b class="">forward: Location.init{custom}</b>) {</font></div><div class=""><font face="Courier" class=""> // .init with label, uses initializer signature with that label<br class=""> profile.shippingAddress = Location(<b class="">forward</b>)<br class=""> }<br class=""> mutating func setBillingAddress(<b class="">forward: Location.init{custom}</b>) {<br class=""> profile.billingAddress = Location(<b class="">forward</b>)<br class=""> }<b class=""><br class=""></b> func build() -> Profile {<br class=""> return profile<br class=""> }<br class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">let builder = BetterBuilder()</font></div><div class=""><font face="Courier" class="">builder.setUser(</font><span class="" style="font-family: Courier;">givenName: </span><font face="Courier" class="">“David”, familyName: “James”</font><span class="" style="font-family: Courier;">)</span><span class="" style="font-family: Courier;"> </span></div><div class=""><font face="Courier" class=""><br class=""></font>It’s important to note, that the<span class="Apple-converted-space"> </span><u class="">above two structs are equivalent</u>, code completion would be the same and the call site would be same. </div><div class=""><br class=""></div><div class="">Syntax is up for grabs. Suggestions would be appreciated. Can this be converged with <a href="https://github.com/DougGregor/swift-evolution/blob/generalized-naming/proposals/0000-generalized-naming.md" class="">Doug Gregor’s function naming proposal</a> or other proposals in the works?</div><div class=""><br class=""></div><div class="">Mainly, is the<span class="Apple-converted-space"> </span><u class="">idea</u><span class="Apple-converted-space"> </span>interesting? Can we expand it to include plain functions/methods as well (not just initializers).</div><div class=""><br class=""></div><div class=""><div class=""><div class="" style="font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><span class="Apple-style-span" style="border-collapse: separate; line-height: normal; border-spacing: 0px;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><span class="Apple-style-span" style="border-collapse: separate; font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><span class="Apple-style-span" style="border-collapse: separate; font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">David James</div></div></span></div></span></div></span></div></div></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=eLFMrKDT8iBxZ-2Fbnk-2BZqvSchNN-2FvYXdceA0T7VxwkAeNGJeTlIgcJ-2FfxZpsSjAM137G-2FiCXsOtozAsM9n98IZ8qktp-2BzukgzliXzrjTiWhgJkAvbEuANIAUKAIEzzFAryr5olSKB8Q1RTXvX1LCHB2m4JbnTOMjGblYCjlqQApCWajf43LFJmuuiwCrWscSRTvKhju6IVgEHPLMcaCOFxeLE26cY-2FxBKv-2BOyEPzAm50-3D" alt="" width="1" height="1" border="0" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div></blockquote></div><br class=""><div class="">
<div style="color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Verdana; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">David James</div></div></span></div></span></div></span></div></div>
</div>
<br class=""></div></body></html>