<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 4, 2015, at 1:24 PM, Erica Sadun <<a href="mailto:erica@ericasadun.com" class="">erica@ericasadun.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-size: 15px;" class=""><b class="">PROBLEM: </b>With many Apple-supplied classes, typical initializers fail to fully set up an instance for use. Here's one example:</div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class=""><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">let</span> task = <span style="color: rgb(112, 61, 170);" class="">NSTask</span>()</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="" class="">task.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">launchPath</span><span style="" class=""> = </span>"/usr/bin/mdfind"</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="" class="">task.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">arguments</span><span style="" class=""> = [</span>"kMDItemDisplayName == *.playground"<span style="" class="">]</span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class="">task.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">standardOutput</span> = pipe</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><br class=""></div></div><div style="font-size: 15px;" class="">Here's another:</div><div style="font-size: 15px;" class=""><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""><br class=""></span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> questionLabel = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UILabel</span>()</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">questionLabel<span style="" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">textAlignment</span><span style="" class=""> = .</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">Center</span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">questionLabel</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">font</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIFont</span>(name:<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"DnealianManuscript"</span>, size: <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">72</span>)</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">questionLabel</span>.text = currentQuestion.questionText</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">questionLabel<span style="" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">numberOfLines</span><span style="" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class=""><br class=""></span></div><div style="font-size: 15px;" class="">You end up with stodgy repetitive code that turns into a blocky hard-to-follow clump. Here are some of my complaints:</div><div style="font-size: 15px;" class=""><ul class=""><li class="">This code feels unnecessarily redundant </li><li class="">This code visually stacks. The task/task/task and questionLabel/questionLabel/questionLabel blocks draw attention away from the actual set-up these lines of code are intended to do. </li><li class="">The extra symbol verbiage goes against common Swift style. For example, when the context is clear, prefer <i class="">.whitespaceAndNewlineCharacterSet</i> to <i class="">NSCharacterSet.whitespaceAndNewlineCharacterSet</i>.</li></ul></div><div class=""><span style="font-size: 15px;" class="">Further, if you have many instances to set up there's no clear way to differentiate unrelated set-up groups other than inserting </span><span style="font-size: 15px;" class="">whitespace gaps or building custom factory functions.</span></div><div class=""><br class=""></div><div style="font-size: 15px;" class=""><b class="">PROPOSED SOLUTION: </b>What do you think about creating setup closures that modify initializers and automatically introduce self-references. For example the NSTask() initialization might look something like this instead:</div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class=""><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> task = NSTask()>>{</div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""> launchPath = <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"/usr/bin/mdfind"</span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="" class=""> arguments = [</span>"kMDItemDisplayName == *.playground"<span style="" class="">]</span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class=""> standardOutput = <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">pipe</span></div><div style="margin: 0px; font-size: 19px; line-height: normal; font-family: Menlo;" class="">}</div></div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class="">In this example, the braces are scoped to the instance as self, enabling the properties to entirely drop their prefixes and be grouped together for set-up.</div></div></div></blockquote><br class=""></div><div>In Smalltalk and Dart, you can do this with method cascades, which apply multiple methods to the same 'self'. In Dart they use '..' for this:</div><div><br class=""></div><div>let task = NSTask()</div><div> ..launchPath = "..."</div><div> ..arguments = [...]</div><div> ..standardOutput = pipe</div><div><br class=""></div><div>The nice thing about that is that it's a bit more generally applicable than just initialization.</div><div><br class=""></div><div>-Joe</div><br class=""></body></html>