<div dir="ltr">Yes, that&#39;s what I suggest:<div><br></div><div>*required initializers*: must call super (autogenerated for base class)</div><div>*convenience initializers*: must assign to self via another initializer (including via subclass)</div><div><br></div><div>(with the minor technical correction that convenience initializers, as is now, are able to call other convenience initializers)</div><div><br></div><div>Although note that inheritance rules for &quot;class cluster&quot; initializers will be very different, the reason being that regular convenience init is essentially</div><div><br></div><div>init(x...) -&gt; Self {</div><div>   self = Self(y: ...)<br><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">which is covariant enough to be able to be inherited, while &quot;class cluster init&quot; is</div><div class="gmail_extra"><br></div><div class="gmail_extra">init (x...) -&gt; BaseClass {</div><div class="gmail_extra">    self = SomeConcreteClass(y: ...)</div><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">which is not covariant.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 8, 2015 at 1:06 AM, Riley Testut <span dir="ltr">&lt;<a href="mailto:rileytestut@gmail.com" target="_blank">rileytestut@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><div>So instead of chaining convenience initializers to required ones via self.init(), you’d recommend instead assigning them directly to self? I think this would be a nice change; currently it can be hard to know the correct method of chaining initializers (at least as far as I’ve seen when attempting to explain it to newcomers).</div><div><br></div><div>Proposed new rules for initialization:</div><div><br></div><div>required initializers: must call super (unless base class)</div><div>convenience initializers: must assign to self via a required initializer</div><div><br></div><div>I think this also would help with the confusion of why convenience methods can’t call super, but required ones can, since now convenience methods can’t chain to <i>any</i> initializers directly. Thoughts from others?</div><div><br></div></span><div><blockquote type="cite"><span class=""><div>On Dec 7, 2015, at 1:17 PM, ilya &lt;<a href="mailto:ilya.nikokoshev@gmail.com" target="_blank">ilya.nikokoshev@gmail.com</a>&gt; wrote:</div><br></span><div><span class=""><div dir="ltr">more precisely like this for regular convenience initializers<div><br></div><div>class SomeClass {</div><div><br></div><div>    convenience init(x: Int) {</div><div>        let y = someComputation(x) // &lt;- can&#39;t use self here</div><div>        self = init(y: y) </div><div>        self.configure() // &lt;- can use self here</div><div>    }</div><div>     </div><div>    init(y:Int)  { // designated</div><div>    ...</div><div>    }</div><div>}</div></div></span><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Tue, Dec 8, 2015 at 12:14 AM, ilya <span dir="ltr">&lt;<a href="mailto:ilya.nikokoshev@gmail.com" target="_blank">ilya.nikokoshev@gmail.com</a>&gt;</span> wrote:<br></span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><div dir="ltr">Actually I think self = SomeClass(anotherInit: ...) would be a better syntax of choice for *all* convenience initializers, as it would make intuitively clear that self cannot be used until this call.</div></span><div><div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Tue, Dec 8, 2015 at 12:11 AM, Riley Testut <span dir="ltr">&lt;<a href="mailto:rileytestut@gmail.com" target="_blank">rileytestut@gmail.com</a>&gt;</span> wrote:<br></span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><span class=""><div></div><div>I actually really like the idea of using a convenience initializer for this, it works well with the Swift initiation process. +1 (though maybe a different keyword than &quot;convenience&quot; to show that it actually assigns to self?)</div></span><div><div><span class=""><div><br>On Dec 7, 2015, at 1:07 PM, ilya &lt;<a href="mailto:ilya.nikokoshev@gmail.com" target="_blank">ilya.nikokoshev@gmail.com</a>&gt; wrote:<br><br></div></span><div><div class="h5"><blockquote type="cite"><div><div dir="ltr">I actually like the way it&#39;s done in the Objective-C, where _isa pointer will be changed appropriately.<div><br></div><div>Perhaps it&#39;s actually possible to solve this problem:</div><div><br></div><div>&gt; Unfortunately, this is wasteful; memory is allocated for the base class, and then subsequently replaced with new memory allocated for the appropriate base class. More importantly though, the whole process can be complicated;<br></div><div><br></div><div>by noting that we can formally mark this initializer as convenience initializer, and memory allocation doesn&#39;t need to happen until we hit a designated initializer. So far I see no reason why this cannot be made to work:</div><div><br></div><div>class Cluster {</div><div><br></div><div>    convenience init(parameters) {</div><div>        if stuff {</div><div>            self = _Cluster1(...)</div><div>        } else {</div><div>            self = _Cluster2(...)</div><div>        }</div><div><br></div><div>        // it&#39;s safe to continue with second init phase</div><div>        self.configure(...)</div><div>   }</div><div>}</div><div><br></div><div>class _Cluster1 { </div><div>    init(parameters: ...) { ... } // designated init, guaranteed never to call convenience inits</div><div>}</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 7, 2015 at 11:55 PM, David Owens II via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Well, the basic idea of a class cluster is to hide the internal implementation. The caller of the API is still supposed to get back an instance of the clustered type (or that conforms to the interface at least).</div><div><br></div><div>It’s still possible to create class clusters though; here’s an example playground:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="Menlo">private protocol _Cluster {<br>    func description() -&gt; String<br>}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">class Cluster {</font></div><div><font face="Menlo">    </font></div><div><font face="Menlo">    private var _instance: _Cluster</font></div><div><font face="Menlo">    </font></div><div><font face="Menlo">    init(name: String) {</font></div><div><font face="Menlo">        _instance = _ClusterString(name: name)</font></div><div><font face="Menlo">    }</font></div><div><font face="Menlo">    </font></div><div><font face="Menlo">    init(value: Int) {</font></div><div><font face="Menlo">        _instance = _ClusterValue(value: value)</font></div><div><font face="Menlo">    }</font></div><div><font face="Menlo">    </font></div><div><font face="Menlo">    func description() -&gt; String {</font></div><div><font face="Menlo">        return _instance.description()</font></div><div><font face="Menlo">    }</font></div><div><font face="Menlo">}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">private class _ClusterString: _Cluster {</font></div><div><font face="Menlo">    private var name: String</font></div><div><font face="Menlo">    init(name: String) { <a href="http://self.name/" target="_blank">self.name</a> = name }</font></div><div><font face="Menlo">    func description() -&gt; String {</font></div><div><font face="Menlo">        return &quot;_ClusterString: \(name)&quot;</font></div><div><font face="Menlo">    }</font></div><div><font face="Menlo">}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">private class _ClusterValue: _Cluster {</font></div><div><font face="Menlo">    private var value: Int</font></div><div><font face="Menlo">    init(value: Int) { self.value = value }</font></div><div><font face="Menlo">    func description() -&gt; String {</font></div><div><font face="Menlo">        return &quot;_ClusterValue: \(value)&quot;</font></div><div><font face="Menlo">    }</font></div><div><font face="Menlo">}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">let s = Cluster(name: &quot;a string&quot;)</font></div><div><font face="Menlo">s.description()</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">let v = Cluster(value: 12)</font></div><div><font face="Menlo">v.description()</font></div></blockquote><div><br></div><div><br></div><div>The implementation is different from how ObjC implements class clusters, but the end result is nearly identical in functionality. If Swift had a form of function redirection, this pattern could be supported with less boiler-plate. However, I don’t believe this proposal is necessary to support class clusters.</div><div><br></div><div>-David</div><div><br></div><div><br></div><div><blockquote type="cite"><div><div><div>On Dec 7, 2015, at 12:19 PM, Riley Testut via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br></div></div><div><div><div><div dir="ltr"><span>Happy Monday everyone!</span><div><br></div><div>I wrote up a prototype proposal, which is probably best viewed on GitHub (<a href="https://github.com/rileytestut/swift-proposals/blob/master/class-cluster.md" target="_blank">https://github.com/rileytestut/swift-proposals/blob/master/class-cluster.md</a>). But for convenience, I’ve included it in this email body as well. Hopefully someone else thinks this would be an idea worth considering :-) </div><div><br></div><div><div><b>## Introduction</b></div><div><br></div><div>Throughout its frameworks, Apple makes use of the “class cluster” pattern as a means to separate the public API out from the (potentially complex) internal representations of the data. Clients of the API simply use the public API, while under the hood a different implementation is chosen to most efficiently represent the provided initialization parameter values.</div><div><br></div><div>Unfortunately, because initializers in Swift are not methods like in Objective-C, there is no way to specify what the actual return value should be (short of returning nil for failable initializers). This makes it *impossible* to actually implement the class cluster pattern in Swift.</div><div><br></div><div><b>## Motivation</b></div><div><br></div><div>While developing my own Swift framework, I found myself wanting to provide a similar functionality. For the client of said framework, I wanted them to be able to create an instance of an essentially abstract class, and be returned a private subclass instance suited best for handling whatever input initialization parameters they provided. It didn’t make sense given the circumstances to ask the user to decide which class would be the best representation of the data; it should “just work”.</div><div><br></div><div>Additionally, the class cluster pattern can make backwards compatibility significantly easier; instead of littering your code with branches for different versions of an OS, you could instead have one if/switch statement to determine the appropriate subclass for the current OS you’re running on. This allows the developer to trivially keep legacy code for older platforms while taking advantage of new APIs/designs, and also without changing *any* client code. An example of the class cluster pattern being used for this reason can be seen here: <a href="http://www.xs-labs.com/en/blog/2013/06/18/ios7-new-ui-strategies/" target="_blank">http://www.xs-labs.com/en/blog/2013/06/18/ios7-new-ui-strategies/</a></div><div><br></div><div><b>## Proposed solution</b></div><div><br></div><div>I propose that we allow for implementation of the class cluster pattern by providing a way to (at run time) specify the actual type that should be initialized depending on the provided initialization parameters.</div><div><br></div><div><b>## Detailed design</b></div><div><br></div><div><u>Introduce a new class method that can return an appropriate type that should be used for initialization, depending on the provided initialization parameters.</u></div><div><br></div><div>This is what I believe to be the most clean solution, and with (assumedly) minimal impact on the existing nature of Swift’s initialization process. To ensure this remains safe, the only types allowed to be returned should be subclasses of the parent class (such as returning a __NSArrayI for NSArray). Notably, beyond this method, everything else remains the same; all this does is change what class the initializer is called on initially.</div><div><br></div><div>Here is an ideal implementation gist:</div><div><a href="https://gist.github.com/rileytestut/0e6e80d3f22b845502e7" target="_blank">https://gist.github.com/rileytestut/0e6e80d3f22b845502e7</a><br></div><div><br></div><div><b>## Impact on existing code</b></div><div><br></div><div>There will be zero impact on existing code; if the proposed class method is not implemented, then it will default to simply initializing the “base” class, as it always has.</div><div><br></div><div><b>## Alternatives considered</b></div><div><br></div><div><u>Allow for return values in initializers</u></div><div><br></div><div>This is essentially how most class cluster patterns are implemented in Objective-C. Inside the init method, the class inspects the provided parameters, then assigns self to an instance of the appropriate subclass. Unfortunately, this is wasteful; memory is allocated for the base class, and then subsequently replaced with new memory allocated for the appropriate base class. More importantly though, the whole process can be complicated; it can be very easy to make an infinite recursive loop by calling [super init] in the subclass, which then assigns self to a new instance of the subclass, which then calls [super init]…etc. </div><div><br></div><div>tl;dr; this method would work, but would be somewhat inconvenient to implement.</div><div><br></div><div><u>Class function to return appropriate instance</u></div><div><br></div><div>This is probably the simplest approach: simply make a class function that returns an instance of the appropriate class given a few input parameters. This totally works, but it means consumers of the API have to remember to use the class method instead of the initializer. Even if all initializers for the class were marked private, it would be strange to have the dissonance between using initializers and class methods to instantiate types in code. The consumer should not have to know about *any* of the implementation details; everything should “just work”. Forcing them to use alternative means to instantiate objects breaks this philosophy, IMO.</div><div><br></div><div><u>Derive from Objective-C base class</u></div><div><br></div><div>Another option is to simply derive from an Objective-C base class, and this is actually what I am doing right now in my framework. Unfortunately, there is one significant drawback: because the initialization is happening in Objective-C, you can only provide Objective-C compatible types for the initialization parameters (so no Swift structs for you!). Additionally, this (obviously) means whatever code is using it is limited to systems with Objective-C support, so it is not as portable as a pure-Swift solution.</div><div><br></div></div></div></div></div><span><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43pXkLx-2B36P-2BPNJufHeY0dgfJy-2FEEfVU-2BGToZLkQbIjT-2FmOX-2B0abbKs-2FNDhBof5llBhSXMDYkcFgZBmwFO5wLwPTL8KadicwG6lI2micqenfwRvVSTW7-2FnhOY7UX6sDSNiHaAzcvFhpXPsWcRQmJapRCh1CUj288XlYofAjeokRgvWvBjc5AnNR9s70BYOPNy3w-3D-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"> _______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54MEtNNixfjCN5oS2qp23gBGzYr9p2j3OPnToUsR4fDJzwhG4UgoPpQIyz3ThMe5BFaVEWwyRP4QmlweGv64ZRlcN7q3XvEwZNQE7rJwL6ag-2FRP6jZIOjBKsL8ee-2FCdG2UXrFUoxMetotpyZzIme6JDcLqCHHlpxguLrm93sVWCgPyeLquA2VXyROmigBL69WPA-3D" alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></blockquote></div></div></div></blockquote></div></div></div></div></div></blockquote></div></div></div></div></blockquote></div></div></div></blockquote></div></div></blockquote></div><br></div></div></div>