<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=""><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="font-variant-ligatures: no-common-ligatures; color: #000000" class="">task.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">launchPath</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #000000" class="">task.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">arguments</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = [</span>"kMDItemDisplayName == *.playground"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">textAlignment</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">numberOfLines</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> arguments = [</span>"kMDItemDisplayName == *.playground"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" 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 style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class=""><b class="">ADVANTAGES AND DISADVANTAGES:</b></div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class="">The advantages I see are as follows:</div><div style="font-size: 15px;" class=""><ul class=""><li class="">additional setup becomes integrated into initialization</li><li class="">code is streamlined, a general Swift ideal</li><li class="">the indented scope provides a visual emphasis of the single task being addressed</li><li class="">allows easier top-level initialization for global values, especially for Swift playgrounds.</li></ul></div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class="">The disadvantages I imagine would include:</div><div style="font-size: 15px;" class=""><ul class=""><li class="">the need for a new operator to disambiguate trailing closures from setup</li><li class="">compiler tweaking so `self`-references within the closure work without constant copies for value types</li></ul></div><div style="font-size: 15px;" class=""><br class=""></div><div style="font-size: 15px;" class="">Thoughts?</div><div style="font-size: 15px;" class=""><br class=""></div><div class=""><br class=""></div></body></html>