<div dir="ltr"><div style="font-size:13px">Hi all,</div><div style="font-size:13px"><br></div><div style="font-size:13px">I&#39;m a new member of the list, so apologies if this is a duplicate of an existing idea or if there&#39;s already a way to do this in Swift 2.1 that I&#39;ve missed.</div><div style="font-size:13px"><br></div><div style="font-size:13px">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 style="font-size:13px"><br></div><div style="font-size:13px">There&#39;s an invisible threshold in Swift init() functions marking this transition. In derived classes it&#39;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 style="font-size:13px"><br></div><div style="font-size:13px">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&#39;t be refactored into an instance function because the instance doesn&#39;t exist yet. The instance function may not even require the use of any properties of the type.</div><div style="font-size:13px"><br></div><div style="font-size:13px">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&#39;t currently happening.</div><div style="font-size:13px"><br></div><div style="font-size:13px">I&#39;m guessing there are multiple contributing factors for this: the combinatorial explosion of possible control flow paths with functions (particularly if they&#39;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&#39;t exist without allocation. I don&#39;t know the reasons but I&#39;d be interested to learn them.</div><div style="font-size:13px"><br></div><div style="font-size:13px">I&#39;m proposing the keyword &#39;selfless&#39; 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 &#39;super&#39; type (in the case of a derived class). It can&#39;t call any instance functions which aren&#39;t themselves selfless.</div><div style="font-size:13px"><br></div><div style="font-size:13px">Example of use:</div><div style="font-size:13px">class FooView : UIView</div><div style="font-size:13px">{</div><div style="font-size:13px">    var property : Int</div><div style="font-size:13px"><br></div><div style="font-size:13px">    init()</div><div style="font-size:13px">    {</div><div style="font-size:13px">        initialiseProperty()</div><div style="font-size:13px">        super.init()</div><div style="font-size:13px">    }</div><div style="font-size:13px"><br></div><div style="font-size:13px">    init(frame:CGRect)</div><div style="font-size:13px">    {</div><div style="font-size:13px">        initialiseProperty()</div><div style="font-size:13px">        super.init(frame)</div><div style="font-size:13px">    }</div><div style="font-size:13px"><br></div><div style="font-size:13px">    selfless func initialiseProperty()</div><div style="font-size:13px">    {</div><div style="font-size:13px">        property = 4</div><div style="font-size:13px">    }</div><div style="font-size:13px">}</div><div style="font-size:13px"><br></div><div style="font-size:13px">Is this something of interest?</div><div style="font-size:13px"><br></div><div style="font-size:13px">Regards,</div><div style="font-size:13px">Ross O&#39;Brien</div></div>