<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&#39;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)">&quot;some thing&quot;</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>&quot;some thing!&quot;<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 &#39;printSomething&#39;</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)">&quot;some thing&quot;</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&#39;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)">&quot;some thing&quot;</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&#39;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&#39;s argument can have a default value. If a function use it, but the implementation doesn&#39;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)">&quot;some thing&quot;</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&#39;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)">&quot;some thing&quot;</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>