<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">It is not to <i>conform to the same function name but with different signatures</i>. It is the protocol limits in current Swift. You can implement a protocol function with default values. But the protocol can't call the function with the default value. So you have to add another function that has no argument in the protocol and change the implementation.<br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">current code:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(187,44,162)">protocol<span style="color:rgb(0,0,0)"> A {</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>)</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">}</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">struct</span> Foo:<span style="color:rgb(79,129,135)">A</span> {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span> = <span style="color:rgb(209,47,27)">"some thing"</span>) {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"> <span style="color:rgb(61,29,129)">print</span>(something)</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"> }</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">}</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(49,89,93)"><span style="color:rgb(79,129,135)">Foo</span><span style="color:rgb(0,0,0)">().</span>printSomething<span style="color:rgb(0,0,0)">() </span><span style="color:rgb(0,132,0)">// some thing</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(79,129,135)">Foo</span><span style="color:rgb(0,0,0)">() </span><span style="color:rgb(187,44,162)">as</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(79,129,135)">A</span><span style="color:rgb(0,0,0)">).printSomething() </span>// Missing argument for parameter #1 in call</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><br></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">default value part is widely discussed in <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160111/006798.html">this thread</a>, including other approaches like protocol extension.</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">zhaoxin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 18, 2016 at 12:52 AM, J. Cheyo Jimenez <span dir="ltr"><<a href="mailto:cheyo@masters3d.com" target="_blank">cheyo@masters3d.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is very interesting. The first case seems like a bug because the compiler should not let you define that function(). <div><br></div><div>Do you have any actual examples when you would need to conform to the same function name but with different signatures? <div><div class="h5"><br><div><br>On Sunday, January 17, 2016, 肇鑫 via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">This proposal is the summary of the previous protocol function default value proposal and some of my new thinkings all together.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Currently the compiler doesn't stop you defining two functions like:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span> = <span style="color:rgb(209,47,27)">"some thing"</span>) {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"> <span style="color:rgb(61,29,129)">print</span>(something)</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">}</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><br></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">func</span> printSomething() {</p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(61,29,129)">print</span><span style="color:rgb(0,0,0)">(</span>"some thing!"<span style="color:rgb(0,0,0)">)</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><br></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">However, when you call it, an error arises.</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">printSomething() </span>// Ambiguous use of 'printSomething'</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><br></p><p style="margin:0px;line-height:normal"><span style="font-family:georgia,serif">You may say just remove the function that has no argument. But protocol needs it.</span><br></p><p style="margin:0px;line-height:normal"><span style="font-family:georgia,serif"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">protocol<span style="color:rgb(0,0,0)"> A {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething()</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Foo:<span style="color:rgb(79,129,135)">A</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(something)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething() {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">self</span><span style="color:rgb(0,0,0)">.</span>printSomething<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(209,47,27)">"some thing"</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;line-height:normal">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small">If you do't define the no-argument function in protocol A. You can not use the function by (Foo() as A).printSomething().</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">As we all know, a function with default values, can rewrite to two or more equivalent functions. For example,</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Bar {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> add(int1:<span style="color:rgb(112,61,170)">Int</span> = <span style="color:rgb(39,42,216)">1</span>, int2:<span style="color:rgb(112,61,170)">Int</span> = <span style="color:rgb(39,42,216)">2</span>, int3:<span style="color:rgb(112,61,170)">Int</span> = <span style="color:rgb(39,42,216)">3</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(int1 + int2 + int3)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small">is equivalent to </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Bar {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> add(int1:<span style="color:rgb(112,61,170)">Int</span>, int2:<span style="color:rgb(112,61,170)">Int</span>, int3:<span style="color:rgb(112,61,170)">Int</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(int1 + int2 + int3)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> add() {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(49,89,93)">add</span>(<span style="color:rgb(39,42,216)">1</span>, int2: <span style="color:rgb(39,42,216)">2</span>, int3: <span style="color:rgb(39,42,216)">3</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> add(int1:<span style="color:rgb(112,61,170)">Int</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(49,89,93)">add</span>(int1, int2: <span style="color:rgb(39,42,216)">2</span>, int3: <span style="color:rgb(39,42,216)">3</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> add(int1:<span style="color:rgb(112,61,170)">Int</span>, int2:<span style="color:rgb(112,61,170)">Int</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(49,89,93)">add</span>(int1, int2: int2, int3: <span style="color:rgb(39,42,216)">3</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">So my proposal is let compiler or pre-compiler to generate the code internally, without changing the original code, base on the functions that have default values.</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">Then we need not to define multiple functions in a protocol when we need function with default values.</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">new code:</font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">protocol<span style="color:rgb(0,0,0)"> A {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Foo:<span style="color:rgb(79,129,135)">A</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span> = <span style="color:rgb(209,47,27)">"some thing"</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(something)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;line-height:normal">
</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal">If we don't want to change our previous code, we may also need to introduce another keyword defaultValue. This keyword is used only in a protocol to restrict if a function's argument can have a default value. If a function use it, but the implementation doesn't give a default value, or vice versa, an error arises.</p><p style="margin:0px;line-height:normal"><br></p><p style="margin:0px;line-height:normal">new code:</p><p style="margin:0px;line-height:normal"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">protocol<span style="color:rgb(0,0,0)"> A {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span> = defaultValue)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Foo:<span style="color:rgb(79,129,135)">A</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span> = <span style="color:rgb(209,47,27)">"some thing"</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(something)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;line-height:normal"></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small">This keyword is useful. With it, you can still use Swift in old way. Which means you need not to change code like below if you don't want to.</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small">old code:</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">protocol<span style="color:rgb(0,0,0)"> A {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething()</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Foo:<span style="color:rgb(79,129,135)">A</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething(something:<span style="color:rgb(112,61,170)">String</span>) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(61,29,129)">print</span>(something)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"> </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> printSomething() {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">self</span><span style="color:rgb(0,0,0)">.</span>printSomething<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(209,47,27)">"some thing"</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p><p style="margin:0px;line-height:normal"></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">But if you want to write new code. You can just write it in the new way, enjoining the simpler and clearer.</font></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small">zhaoxin</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-family:georgia,serif;font-size:small"><br></span></p></div></div>
</blockquote></div></div></div></div>
</blockquote></div><br></div>