<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)"><p 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></p>
<p 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></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></p>
<p 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></p>
<p 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>}</p>
<p 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></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>private</span><span style="color:rgb(0,0,0)"> </span><span>class</span><span style="color:rgb(0,0,0)"> SubA: </span><span style="color:rgb(79,129,135)">CommonSuper</span><span style="color:rgb(0,0,0)"> {</span></p>
<p 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></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>private</span><span style="color:rgb(0,0,0)"> </span><span>class</span><span style="color:rgb(0,0,0)"> SubB: </span><span style="color:rgb(79,129,135)">CommonSuper</span><span style="color:rgb(0,0,0)"> {</span></p>
<p 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></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></p></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&#39;t know what you&#39;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">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</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 &amp; 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 style="color:#000000"> </span><span>protocol</span><span style="color:#000000"> 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 style="color:#000000"> </span><span style="color:#bb2ca2">extension </span><span style="color:#000000"> </span><span style="color:#000000">PublicProto </span><span style="color:#bb2ca2">where</span><span style="color:#000000"> </span><span style="color:#bb2ca2">Self</span><span style="color:#000000">: 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 style="color:#000000">    </span><span style="color:#bb2ca2">public</span><span style="color:#000000"> </span><span style="color:#bb2ca2">func</span><span style="color:#000000"> publicFn() { </span><span style="color:#31595d">specificPrivateFn</span><span style="color:#000000">() } </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 style="color:#000000"> SA: </span><span style="color:#000000">PublicProto, </span><span>PrivateProto</span><span style="color:#000000"> {</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 style="color:#000000"> SB: </span><span style="color:#000000">PublicProto, </span><span>PrivateProto</span><span style="color:#000000"> {</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>