<div dir="ltr">If you go further, you will realize that Objects are only Structures with some function pointers.<br><br>The differences on structured programming and object-oriented programming can be reduced to a &quot;syntax level&quot; too.<br><br>POP for example encourages more use of multiple (inheritance|composition) than OO. The pattern is more important than syntax.<br><br>However I agree, Swift POP still have many limitations and almost no innovation. There are several proposed improvements to protocols that should arrive in the next versions, maybe then people start to consider POP a true paradigm. For now is just a syntax sugar for <u>Composition over inheritance</u>.</div><br><div class="gmail_quote"><div dir="ltr">Em qua, 17 de fev de 2016 às 01:35, Daniel Tartaglia via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; escreveu:<br></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">And your example is exactly equivalent to the below:<div><br></div><div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">protocol</span> TestProtocol {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num1: <span style="color:#703daa">Int</span> {<span style="color:#bb2ca2">get</span> <span style="color:#bb2ca2">set</span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num2: <span style="color:#703daa">Int</span> {<span style="color:#bb2ca2">get</span> <span style="color:#bb2ca2">set</span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">func</span> doMath(t: <span style="color:#4f8187">TestProtocol</span>) -&gt; <span style="color:#703daa">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">return</span> t.<span style="color:#4f8187">num1</span> + t.<span style="color:#4f8187">num2</span></div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> MyTypeOne: <span style="color:#4f8187">TestProtocol</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num1 = <span style="color:#272ad8">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num2 = <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> MyTypeTwo: <span style="color:#4f8187">TestProtocol</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num1 = <span style="color:#272ad8">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">var</span> num2 = <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span><span style="color:#bb2ca2">func</span> doMath() -&gt; <span style="color:#703daa">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#000000"><span style="white-space:pre-wrap">                </span></span><span style="color:#bb2ca2">return</span><span style="color:#000000"> </span>num2<span style="color:#000000"> - </span>num1</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="white-space:pre-wrap">        </span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> one = <span style="color:#4f8187">MyTypeOne</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> two = <span style="color:#4f8187">MyTypeTwo</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span style="color:#000000">(</span>&quot;MyTypeOne: <span style="color:#000000">\</span>(<span style="color:#31595d">doMath</span><span style="color:#000000">(</span><span style="color:#4f8187">one</span><span style="color:#000000">)</span>)&quot;<span style="color:#000000">)</span></div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span style="color:#000000">(</span>&quot;MyTypeTwo: <span style="color:#000000">\</span>(<span style="color:#4f8187">two</span><span style="color:#000000">.</span><span style="color:#31595d">doMath</span><span style="color:#000000">()</span>)&quot;<span style="color:#000000">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">var</span><span style="color:#000000"> t: </span>TestProtocol<span style="color:#000000"> = </span>two</div></div></div><div style="word-wrap:break-word"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span style="color:#000000">(</span>&quot;TestProtocol <span style="color:#000000">\</span>(<span style="color:#31595d">doMath</span><span style="color:#000000">(</span><span style="color:#4f8187">t</span><span style="color:#000000">)</span>)&quot;<span style="color:#000000">)</span></div><div><span style="color:#000000"><br></span></div><div>What the protocol extension gives you, all that it gives you, is a different syntax for doing the exact same thing. Where your code shines IMHO is when you want to then use the return value as a parameter to yet another function.</div><div><br></div><div><div style="margin:0px;line-height:normal;color:rgb(49,89,93)"><span style="font-family:Menlo;font-size:11px;color:rgb(79,129,135)">two</span><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">.</span><font face="Menlo"><span style="font-size:11px">doMath</span></font><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">().</span><font face="Menlo"><span style="font-size:11px">handleResult</span></font><span style="color:rgb(0,0,0)"><font face="Menlo"><span style="font-size:11px">() </span></font>is far cleaner, and better expresses what is happening, than<font face="Menlo"><span style="font-size:11px"> </span></font></span><font face="Menlo"><span style="font-size:11px">handleResult</span></font><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">(</span><font face="Menlo"><span style="font-size:11px">doMath</span></font><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">(</span><span style="font-family:Menlo;font-size:11px;color:rgb(79,129,135)">one</span><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">))</span></div></div><div><span style="color:#000000"><br></span></div><div><span style="color:#000000">All IMHO of course.</span></div></div></div><div style="word-wrap:break-word"><div><div><br></div><div><blockquote type="cite"><div>On Feb 16, 2016, at 9:28 PM, Jon Hoffman &lt;<a href="mailto:hoffman.jon@gmail.com" target="_blank">hoffman.jon@gmail.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">You are correct that if you are using the protocol type to interact with the instance then the method defined in the protocol extension will be called however if it is type casted to a concrete type which has a method that overrides the method in the protocol extension then the method in the concrete type would be called.  This following code illustrates this:<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">protocol</span> TestProtocol {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num1: <span style="color:#703daa">Int</span> {<span style="color:#bb2ca2">get</span> <span style="color:#bb2ca2">set</span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num2: <span style="color:#703daa">Int</span> {<span style="color:#bb2ca2">get</span> <span style="color:#bb2ca2">set</span>}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">extension</span><span> </span>TestProtocol<span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">func</span> doMath() -&gt; <span style="color:#703daa">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:#bb2ca2">return</span> <span style="color:#4f8187">num1</span> + <span style="color:#4f8187">num2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> MyTypeOne: <span style="color:#4f8187">TestProtocol</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num1 = <span style="color:#272ad8">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num2 = <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">struct</span> MyTypeTwo: <span style="color:#4f8187">TestProtocol</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num1 = <span style="color:#272ad8">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">var</span> num2 = <span style="color:#272ad8">2</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">func</span> doMath() -&gt; <span style="color:#703daa">Int</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:#bb2ca2">return</span> <span style="color:#4f8187">num2</span> - <span style="color:#4f8187">num1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> one = <span style="color:#4f8187">MyTypeOne</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> two = <span style="color:#4f8187">MyTypeTwo</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span>(</span>&quot;MyTypeOne: <span>\</span>(<span style="color:#4f8187">one</span><span>.</span><span style="color:#31595d">doMath</span><span>()</span>)&quot;<span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span>(</span>&quot;MyTypeTwo: <span>\</span>(<span style="color:#4f8187">two</span><span>.</span><span style="color:#31595d">doMath</span><span>()</span>)&quot;<span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#bb2ca2">var</span><span> t: </span>TestProtocol<span> = </span>two</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:#3d1d81">print</span><span>(</span>&quot;TestProtocol <span>\</span>(<span style="color:#4f8187">t</span><span>.</span><span style="color:#31595d">doMath</span><span>()</span>)&quot;<span>)</span></div></div><div><br></div><div>The output from this code would be:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>MyTypeOne: 3</b></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>MyTypeTwo: 1</b></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>TestProtocol 3</b></div></div><div><br></div><div>This shows that when calling the doMath() on the MyTypeTwo instance calls the method defined in the MyTypeTwo structure however if we call the method on the same instance but it being typecast as the protocol will call the method in the protocol.</div><div><br></div><div>For the most part we would access the types though the interface provided by the protocol however using the “is” keyword we can check if a type is an instance of a particular concrete type or use the typecast operator to downcast the instance.  This would give us access to the method that is defined in the concrete type.  This can certainly be useful in certain situations.  I do agree that these situation should be avoided however it is something that can be done which we can not do with global functions.  I was just trying to list some of the differences between protocol extensions and global functions. Honestly, and this is my personal opinion, I believe there is significant difference between protocol extensions and global functions:  avoiding the global scope, using constraints, better code organization to name a couple, overriding, self parameter, better code manageability just to name some of them.</div><div><br></div><div>Protocol Extensions are there so we can add functionality to a group of types that conform to a specific protocol that meets any constraints defined by the extension.  This allows us to avoid duplicate code similar to what we did with super classes in the OOP world.  However with protocols we can create very specific protocols rather than large monolithic superclasses.  Without protocol extensions we would have to revert to creating global functions however, in my opinion once again, the protocol extensions is a better option.  That is what I was trying to get across.</div><div><br></div><div>Jon</div><div> </div><div><br><div><blockquote type="cite"><div>On Feb 16, 2016, at 8:30 PM, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>&gt; wrote:</div><br><div><div><blockquote type="cite">We can override functionality provided from a protocol extension if we need specific functionality for a particular type.<br></blockquote><br>No you can&#39;t. Protocol extension methods (that is, methods declared only in an extension, as opposed to default implementations, which are both declared in the protocol and defined in an extension) are statically dispatched, so calling them on the protocol witness will always use the extension&#39;s implementation, even if some concrete type conforming to the protocol has a different implementation with the same name and signature.<br><br>(I&#39;ve worked on a proposal in the past that would require keywords to make this behavior clearer, but I&#39;ve had trouble getting it through.)<br><br>In any case, I&#39;m pretty sure that&#39;s what Daniel means when he says extension methods are exactly equivalent to global functions. Sure, they appear after a dot, have an implicit self parameter, and are scoped to a particular type, but there&#39;s nothing dynamic about their behavior—they&#39;re not overridable in any useful sense.<br><br>-- <br>Brent Royal-Gordon<br>Architechies<br><br></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div>_______________________________________________<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>
</blockquote></div>