<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Kevin,<div class=""><br class=""></div><div class="">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><div class=""><br class=""></div><div class="">- David</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 22, 2016, at 1:19 PM, Kevin Greene &lt;<a href="mailto:kgreenek@gmail.com" class="">kgreenek@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><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 class=""></div><div class="gmail_default" style="color:rgb(51,102,102)"><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">public</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">protocol</span><span class=""> PublicProto {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> publicFn()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(187,44,162)" class="">public</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">class</span><span class=""> CommonSuper: </span><span style="color:rgb(79,129,135)" class="">PublicProto</span><span class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">public</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> publicFn() {&nbsp;</span>specificPrivate()<span style="color:rgb(0,132,0)" class="">&nbsp;</span>}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">private</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> specificPrivate() {}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span class="">private</span><span style="" class=""> </span><span class="">class</span><span style="" class=""> SubA: </span><span style="color:rgb(79,129,135)" class="">CommonSuper</span><span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">override</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">private</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> specificPrivate() { </span><span style="color:rgb(0,132,0)" class="">/* ... */</span><span class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span class="">private</span><span style="" class=""> </span><span class="">class</span><span style="" class=""> SubB: </span><span style="color:rgb(79,129,135)" class="">CommonSuper</span><span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">override</span><span class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">private</span><span class=""> </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> specificPrivate() { </span><span style="color:rgb(0,132,0)" class="">/* ... */</span><span class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="">}</span></div></div><div class="gmail_default" style="color:rgb(51,102,102)"><br class=""></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 class=""><div class="gmail_quote">On Wed, Jun 22, 2016 at 11:19 AM, David Ungar via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><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="">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 class=""><br class=""></div><div class="">Here it is in classes:</div><div class=""><br class=""></div><div class="">public class CommonSuper {</div><div class="">&nbsp; public func publicFn() { …&nbsp;specificPrivateFn() &nbsp;… }</div><div class="">&nbsp; private func specificPrivateFn() { }</div><div class="">}</div><div class=""><br class=""></div><div class="">private class SubA {</div><div class="">&nbsp; override private func specificPrivate() { … }</div><div class="">}</div><div class=""><div class="">private class SubB {</div><div class="">&nbsp; override &nbsp;private func specificPrivate() { … }</div><div class="">}</div></div><div class=""><br class=""></div><div class="">I have tried it lots of ways with protocols, and can get none to compile. Here is one:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)" class=""><span class="">public</span><span style="" class=""> </span><span class="">protocol</span><span style="" class=""> PublicProto {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#bb2ca2" class="">func</span><span class="">&nbsp;publicFn()</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">private</span><span class=""> </span><span style="color:#bb2ca2" class="">protocol</span><span class=""> PrivateProto&nbsp;{</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#bb2ca2" class="">func</span><span class=""> specificPrivateFn()</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span style="color:#bb2ca2" class="">public</span><span style="" class=""> </span><span style="color:#bb2ca2" class="">extension&nbsp;</span><span style="" class="">&nbsp;</span><span style="" class="">PublicProto&nbsp;</span><span style="color:#bb2ca2" class="">where</span><span style="" class=""> </span><span style="color:#bb2ca2" class="">Self</span><span style="" class="">: PrivateProto&nbsp;{ </span><span class="">// 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)" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:#bb2ca2" class="">public</span><span style="" class=""> </span><span style="color:#bb2ca2" class="">func</span><span style="" class="">&nbsp;publicFn() { </span><span style="color:#31595d" class="">specificPrivateFn</span><span style="" class="">() } </span><span class="">// 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" class=""><span class="">}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;min-height:14px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="color:#bb2ca2" class="">private struct</span><span style="" class=""> SA:&nbsp;</span><span style="" class="">PublicProto, </span><span class="">PrivateProto</span><span style="" class="">&nbsp;{</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#bb2ca2" class="">private</span><span class=""> </span><span style="color:#bb2ca2" class="">func</span><span class="">&nbsp;<span style="color:rgb(49,89,93)" class="">specificPrivateFn</span>() {}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="color:#bb2ca2" class="">private struct</span><span style="" class=""> SB:&nbsp;</span><span style="" class="">PublicProto, </span><span class="">PrivateProto</span><span style="" class="">&nbsp;{</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">&nbsp; &nbsp; </span><span style="color:#bb2ca2" class="">private</span><span class=""> </span><span style="color:#bb2ca2" class="">func</span><span class="">&nbsp;<span style="color:rgb(49,89,93)" class="">specificPrivateFn</span>() {}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><span class=""><br class=""></span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class="">What am I doing wrong?</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class="">Thanks,</div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Menlo" class="">- David</div></div><br class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></body></html>