<div dir="ltr"><div class="gmail_default" style="color:rgb(51,102,102)">Oh gotcha. I totally misunderstood the question. Sorry about that!</div><div class="gmail_default" style="color:rgb(51,102,102)">I didn't actually realize you can extend protocols. Cool stuff! For anybody else who is uninitiated:</div><div class="gmail_default" style="color:rgb(51,102,102)"><a href="https://www.raywenderlich.com/109156/introducing-protocol-oriented-programming-in-swift-2">https://www.raywenderlich.com/109156/introducing-protocol-oriented-programming-in-swift-2</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 22, 2016 at 3:17 PM, David Ungar <span dir="ltr"><<a href="mailto:ungar@mac.com" target="_blank">ungar@mac.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Kevin,<div><br></div><div>Thank you so much for helping me out. I wasn’t clear, but I’m hoping to find a solution that uses only value types and protocols.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>- David</div></font></span><div><div class="h5"><div><br></div><div><br><div><blockquote type="cite"><div>On Jun 22, 2016, at 1:19 PM, Kevin Greene <<a href="mailto:kgreenek@gmail.com" target="_blank">kgreenek@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="color:rgb(51,102,102)">You could have your common superclass implement your protocol. E.g...</div><div class="gmail_default" style="color:rgb(51,102,102)"><br></div><div class="gmail_default" style="color:rgb(51,102,102)"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span><span> </span><span style="color:rgb(187,44,162)">protocol</span><span> PublicProto {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">func</span><span> publicFn()</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span><span> </span><span style="color:rgb(187,44,162)">class</span><span> CommonSuper: </span><span style="color:rgb(79,129,135)">PublicProto</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">public</span><span> </span><span style="color:rgb(187,44,162)">func</span><span> publicFn() { </span>specificPrivate()<span style="color:rgb(0,132,0)"> </span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">private</span><span> </span><span style="color:rgb(187,44,162)">func</span><span> specificPrivate() {}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>private</span><span> </span><span>class</span><span> SubA: </span><span style="color:rgb(79,129,135)">CommonSuper</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">override</span><span> </span><span style="color:rgb(187,44,162)">private</span><span> </span><span style="color:rgb(187,44,162)">func</span><span> specificPrivate() { </span><span style="color:rgb(0,132,0)">/* ... */</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>private</span><span> </span><span>class</span><span> SubB: </span><span style="color:rgb(79,129,135)">CommonSuper</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:rgb(187,44,162)">override</span><span> </span><span style="color:rgb(187,44,162)">private</span><span> </span><span style="color:rgb(187,44,162)">func</span><span> specificPrivate() { </span><span style="color:rgb(0,132,0)">/* ... */</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div class="gmail_default" style="color:rgb(51,102,102)"><br></div><div class="gmail_default" style="color:rgb(51,102,102)">I don't know what you're doing specifically, but I would guess that a cleaner approach would likely be to get rid of the super class entirely, and pull the shared logic from your two subclasses into a separate object that both classes instantiate, or have injected. Then have your two classes implement PublicProto directly. That discussion would probably be best had on another mailing list though.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 22, 2016 at 11:19 AM, David Ungar via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I love protocol-oriented programming because of the guarantees that come with value types. But I cannot figure out how to do the same factoring I can do with the class side of the the language. I want to factor out common code into a public method that calls specific code in a private method & I want to do this for value types.<div><br></div><div>Here it is in classes:</div><div><br></div><div>public class CommonSuper {</div><div> public func publicFn() { … specificPrivateFn() … }</div><div> private func specificPrivateFn() { }</div><div>}</div><div><br></div><div>private class SubA {</div><div> override private func specificPrivate() { … }</div><div>}</div><div><div>private class SubB {</div><div> override private func specificPrivate() { … }</div><div>}</div></div><div><br></div><div>I have tried it lots of ways with protocols, and can get none to compile. Here is one:</div><div><br></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>public</span><span> </span><span>protocol</span><span> PublicProto {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">func</span><span> publicFn()</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">private</span><span> </span><span style="color:#bb2ca2">protocol</span><span> PrivateProto {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">func</span><span> specificPrivateFn()</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">extension </span><span> </span><span>PublicProto </span><span style="color:#bb2ca2">where</span><span> </span><span style="color:#bb2ca2">Self</span><span>: PrivateProto { </span><span>// Error: Extension cannot be declared public because its generic requirement uses a private type</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span> </span><span style="color:#bb2ca2">public</span><span> </span><span style="color:#bb2ca2">func</span><span> publicFn() { </span><span style="color:#31595d">specificPrivateFn</span><span>() } </span><span>// Error: Cannot declare a public instance method in an extension with private requirements</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px"><span></span><br></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">private struct</span><span> SA: </span><span>PublicProto, </span><span>PrivateProto</span><span> {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">private</span><span> </span><span style="color:#bb2ca2">func</span><span> <span style="color:rgb(49,89,93)">specificPrivateFn</span>() {}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">private struct</span><span> SB: </span><span>PublicProto, </span><span>PrivateProto</span><span> {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#bb2ca2">private</span><span> </span><span style="color:#bb2ca2">func</span><span> <span style="color:rgb(49,89,93)">specificPrivateFn</span>() {}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span>}</span></div></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo">What am I doing wrong?</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo">Thanks,</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo">- David</div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>