<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 class="">The problem exists only if you have multiple designated initializers.</div><div class=""><br class=""></div><div class="">For other cases, you can use convenience initializers.</div><div class=""><div style="margin: 0px; font-size: 11px; 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: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> FooView : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIView</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> property : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>convenience<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>init<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CGRect</span>())</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">override</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CGRect</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">property</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: frame)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">required</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>?(coder aDecoder: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSCoder</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">property</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(coder: aDecoder)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">For the case with multiple designated initializers, you could use lazy initialized properties.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> FooView : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIView</span></div><div style="margin: 0px; line-height: normal;" class="">{</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">lazy</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> property : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span> = {</div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Can use self here: <span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>)"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span></div><div style="margin: 0px; line-height: normal;" class=""> }()</div><p style="margin: 0px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>convenience<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>init<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CGRect</span>())</div><div style="margin: 0px; line-height: normal;" class=""> }</div><p style="margin: 0px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">override</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CGRect</span>)</div><div style="margin: 0px; line-height: normal;" class=""> {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(frame: frame)</div><div style="margin: 0px; line-height: normal;" class=""> }</div><p style="margin: 0px; line-height: normal; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">required</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>?(coder aDecoder: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSCoder</span>) {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(coder: aDecoder)</div><div style="margin: 0px; line-height: normal;" class=""> }</div><div style="margin: 0px; line-height: normal;" class="">}</div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 16 Dec 2015, at 00:52, Ross O'Brien 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 dir="ltr" class=""><div class="">Hi all,</div><div class=""><br class=""></div><div class="">I'm a new member of the list, so apologies if this is a duplicate of an existing idea or if there's already a way to do this in Swift 2.1 that I've missed.</div><div class=""><br class=""></div><div class="">In Objective C, and C-like languages, an initialiser function represents a stage after allocation of memory where properties are given values. In Swift, init appears to precede (or overlap with) allocation. The benefit of this is that for type-safety reasons, all properties of a type (or new properties of a derived type) can be verified as having values. The disadvantage, and one of the stumbling blocks for those who learned Objective-C, is that until all the properties have values, the instance does not exist and instance functions cannot be called.</div><div class=""><br class=""></div><div class="">There's an invisible threshold in Swift init() functions marking this transition. In derived classes it's the point where super.init() is called - after the derived type has provided initial values, but before any type functions can be called.</div><div class=""><br class=""></div><div class="">Some types have multiple initialisers, and may be duplicating a lot of code in those distinct inits before they cross the threshold. This code can't be refactored into an instance function because the instance doesn't exist yet. The instance function may not even require the use of any properties of the type.</div><div class=""><br class=""></div><div class="">If the compiler can read an init function and its varied control flow and determine a threshold where all properties have values, presumably it can read the code of any function called before that threshold, determine which properties they read and which they assign to, and provide a warning if a path assigns to a constant a second time, etc.. But this isn't currently happening.</div><div class=""><br class=""></div><div class="">I'm guessing there are multiple contributing factors for this: the combinatorial explosion of possible control flow paths with functions (particularly if they're recursive); the possibility that the function calls are used by the compiler to mark the end of a control flow path, by which point it can determine whether everything has a value; the function genuinely can't exist without allocation. I don't know the reasons but I'd be interested to learn them.</div><div class=""><br class=""></div><div class="">I'm proposing the keyword 'selfless' for a function which could be called before the threshold. It either only uses local properties or properties belonging to the type - never to the 'super' type (in the case of a derived class). It can't call any instance functions which aren't themselves selfless.</div><div class=""><br class=""></div><div class="">Example of use:</div><div class="">class FooView : UIView</div><div class="">{</div><div class=""> var property : Int</div><div class=""><br class=""></div><div class=""> init()</div><div class=""> {</div><div class=""> initialiseProperty()</div><div class=""> super.init()</div><div class=""> }</div><div class=""><br class=""></div><div class=""> init(frame:CGRect)</div><div class=""> {</div><div class=""> initialiseProperty()</div><div class=""> super.init(frame)</div><div class=""> }</div><div class=""><br class=""></div><div class=""> selfless func initialiseProperty()</div><div class=""> {</div><div class=""> property = 4</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">Is this something of interest?</div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Ross O'Brien</div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=tTTJ5sn5y0uc3ODSZa-2BndLNwXCDS7T2cq5OlDDhG0RuAkiCipI7-2FpVcynLSXev2clRvDwtZkFWFTYl0rQIXWvOwKJH1OEEfTcwP0qmrAief8FCUXbWM1r-2B6MscGwqBgDu-2FE2pxDrbOsjdSbj0-2FmeVSnm7Y4NNjKMUsfayXDtU26ePDXJMIgtZQJxtNPJEq1SvR5wiy4QuMvMcv4X6W-2B6mXxY5WpMP8vwM-2BcsOK9CiNA-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<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=""></div></body></html>