<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Worth starting another thread discussing the pros and cons of method forwarding in Swift? I think that would definitely solve the class cluster problem</div><div><br>On Dec 7, 2015, at 1:06 PM, David Owens II &lt;<a href="mailto:david@owensd.io">david@owensd.io</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div class="">You still need everything else if can return arbitrary types from init(). The only thing you don’t get from Swift today is the forwarding that is required for the _Cluster protocol. However, that problem is a more general one that has been discussed by many outside of the swift-evolution (surprised there is no proposal for that yet).</div><div class=""><br class=""></div><div class="">If forwarding was in the language, there is no difference between the implementations other than the requirement of the _instance variable.</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Dec 7, 2015, at 12:59 PM, Jacob Bandes-Storch &lt;<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="white-space:pre-wrap" class="">To me it seems a simpler solution would be to allow `return`ing a different object (or assigning to self) from inside init.</div><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Dec 7, 2015 at 12:55 PM David Owens II via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">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 class=""><br class=""></div><div class="">It’s still possible to create class clusters though; here’s an example playground:</div><div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class=""><font face="Menlo" class="">private protocol&nbsp;_Cluster {<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;description() -&gt;&nbsp;String<br class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">class&nbsp;Cluster {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;private&nbsp;var&nbsp;_instance:&nbsp;_Cluster</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;init(name:&nbsp;String) {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;_instance&nbsp;=&nbsp;_ClusterString(name: name)</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;init(value:&nbsp;Int) {</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;_instance&nbsp;=&nbsp;_ClusterValue(value: value)</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;func&nbsp;description() -&gt;&nbsp;String&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;_instance.description()</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">private&nbsp;class&nbsp;_ClusterString:&nbsp;_Cluster&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;private&nbsp;var&nbsp;name:&nbsp;String</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;init(name:&nbsp;String) {&nbsp;<a href="http://self.name/" target="_blank" class="">self.name</a>&nbsp;= name }</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;func&nbsp;description() -&gt;&nbsp;String&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;"_ClusterString:&nbsp;\(name)"</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">private&nbsp;class&nbsp;_ClusterValue:&nbsp;_Cluster&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;private&nbsp;var&nbsp;value:&nbsp;Int</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;init(value:&nbsp;Int) {&nbsp;self.value&nbsp;= value }</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;func&nbsp;description() -&gt;&nbsp;String&nbsp;{</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;"_ClusterValue:&nbsp;\(value)"</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;}</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">let&nbsp;s =&nbsp;Cluster(name:&nbsp;"a string")</font></div><div class=""><font face="Menlo" class="">s.description()</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">let&nbsp;v =&nbsp;Cluster(value:&nbsp;12)</font></div><div class=""><font face="Menlo" class="">v.description()</font></div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">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><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class="">-David</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On Dec 7, 2015, at 12:19 PM, Riley Testut via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div dir="ltr" class=""><span style="font-family:Helvetica;font-size:12px" class="">Happy Monday everyone!</span><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class="">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" class="">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 :-)&nbsp;</div><div style="font-family:Helvetica;font-size:12px" class=""><br class=""></div><div style="font-family:Helvetica;font-size:12px" class=""><div class=""><b class="">## Introduction</b></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><b class="">## Motivation</b></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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" class="">http://www.xs-labs.com/en/blog/2013/06/18/ios7-new-ui-strategies/</a></div><div class=""><br class=""></div><div class=""><b class="">## Proposed solution</b></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><b class="">## Detailed design</b></div><div class=""><br class=""></div><div class=""><u class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Here is an ideal implementation gist:</div><div class=""><a href="https://gist.github.com/rileytestut/0e6e80d3f22b845502e7" target="_blank" class="">https://gist.github.com/rileytestut/0e6e80d3f22b845502e7</a><br class=""></div><div class=""><br class=""></div><div class=""><b class="">## Impact on existing code</b></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><b class="">## Alternatives considered</b></div><div class=""><br class=""></div><div class=""><u class="">Allow for return values in initializers</u></div><div class=""><br class=""></div><div class="">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.&nbsp;</div><div class=""><br class=""></div><div class="">tl;dr; this method would work, but would be somewhat inconvenient to implement.</div><div class=""><br class=""></div><div class=""><u class="">Class function to return appropriate instance</u></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><u class="">Derive from Objective-C base class</u></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div></div></div>
<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: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" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qzb1URyjLMDuX7UgtnRwkYxmLscZh-2FIZitRYHSnJYFMDjV8jUowveRU-2FxFtqd03SS8GUdcWihzuSyVsnnMhG6bJ93kGMqHEHApyxXpmC3qItq-2Fm4YT1XkzWoMxquNy1K1AZ6qAu8s4lb9-2BAPKHIf2wq9F5Gtt7O0Vaskbs-2BFr2y7t1T7PAmEAi5bRxPA-2B9-2Bl2g-3D" alt="" width="1" height="1" border="0" style="min-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="">
</div>
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></div></blockquote></body></html>