<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Hi Haravikk,</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">I just implement a func like your examples. However, I find the result is interesting. </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)"> Good {</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;color:rgb(187,44,162)">extension<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(79,129,135)">Good</span><span style="color:rgb(0,0,0)"> {</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() { <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(49,89,93)">printSomething</span>(<span style="color:rgb(209,47,27)">"Good"</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> Name:<span style="color:rgb(79,129,135)">Good</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)">"John"</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"><span style="color:rgb(187,44,162)">struct</span> Talk:<span style="color:rgb(79,129,135)">Good</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)">"Hahaha"</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"><span style="color:rgb(187,44,162)">var</span> protocols:[<span style="color:rgb(79,129,135)">Good</span>] = [<span style="color:rgb(79,129,135)">Name</span>(), <span style="color:rgb(79,129,135)">Talk</span>()]</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(79,129,135)"><span style="color:rgb(187,44,162)">for</span><span style="color:rgb(0,0,0)"> p </span><span style="color:rgb(187,44,162)">in</span><span style="color:rgb(0,0,0)"> </span>protocols<span style="color:rgb(0,0,0)"> {</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)"> p.</span><span style="color:rgb(49,89,93)">printSomething</span><span style="color:rgb(0,0,0)">() </span>// Good, Good instead of John, Hahaha</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">As you can see, the final result is "Good, Good", not as I expected "John, Hahaha", is that right or is it a bug?</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">zhaoxin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 16, 2016 at 7:04 PM, Haravikk <span dir="ltr"><<a href="mailto:me@haravikk.com" target="_blank">me@haravikk.com</a>></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"><div>I think that the point of allowing defaults in protocols is so that you can assume a default for all types conforming to that protocol. To use your example, if you receive an instance of Name, you can only call printSomething() without arguments if you test that it is an instance of type Name. If instead you test its conformance to the Good protocol (which you might do if there are a lot of different types conforming to Good) then you have to provide a value, because you can’t infer that every possible implementation will have a default.</div><div><br></div><div>Regarding this proposal however I think it might be useful to have a distinction between a protocol function that specifies a default value for all implementations (that they must all conform to) versus one that specifies that implementations must have a default value, but not what that value must be.</div><div><br></div><div>For example, to have a fixed and altered default we currently we have to do things like this:</div><div><br></div><div><div>protocol Protocol {</div><div><span style="white-space:pre-wrap">        </span>func functionWithSpecificDefault(argument:String)</div><div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault(argument:String)</div><div>}</div></div><div><br></div><div>extension Protocol {</div><div><span style="white-space:pre-wrap">        </span>func functionWithSpecificDefault() { self.functionWithSpecificDefault(“Foo”) }</div><div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault() { self.functionWithAnyDefault(“Foo”) }</div><div>}</div><div><br></div><div>class MyClass : Protocol {</div><span style="white-space:pre-wrap">        </span>func functionWithSpecificDefault(argument:String) { /* Implementation here */ }<div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault(argument:String) { /* Implementation here */ }</div><div><br><div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault() { self.functionWithAnyDefault(“Bar”) } // Override default</div><div>}</div><div><br></div><div>Which could be replaced by:</div><div><br></div><div>protocol Protocol {</div><div><span style="white-space:pre-wrap">        </span>func functionWithSpecificDefault(argument:String = “Foo")</div><div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault(argument:String = default)</div><div>}</div><div><br></div><div>class MyClass : Protocol {</div><span style="white-space:pre-wrap">        </span>func functionWithSpecificDefault(argument:String = “Foo") { /* Implementation here */ }<div><span style="white-space:pre-wrap">        </span>func functionWithAnyDefault(argument:String = “Bar") { /* Implementation here */ }</div><div>}</div><div><br></div><div>However, this has the added advantage that implementing functionWithSpecificDefault with a default other than “Foo” would cause a compiler error, while doing so for functionWithAnyDefault would not (but specifying no default at all would, as one is required).</div><div><div class="h5"><br><div><blockquote type="cite"><div>On 16 Jan 2016, at 10:15, 肇鑫 via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">No. Although you protocol's function doesn't has a default parameter value. Your implementation does. So you don't need to define another func function() in your protocol.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">protocol Good {</font></div><div class="gmail_default"><font face="georgia, serif"> func printSomething(something:String)</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">struct Name:Good {</font></div><div class="gmail_default"><font face="georgia, serif"> func printSomething(something: String = "John") {</font></div><div class="gmail_default"><font face="georgia, serif"> print(something)</font></div><div class="gmail_default"><font face="georgia, serif"> }</font></div><div class="gmail_default"><font face="georgia, serif">}</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">Name().printSomething()</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">above code works.</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">zhaoxin</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 16, 2016 at 6:05 PM, Vatsal Manot <span dir="ltr"><<a href="mailto:vatsal.manot@yahoo.com" target="_blank">vatsal.manot@yahoo.com</a>></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"><div>It serves as a better (if not simpler) substitute for the following pattern:</div><div> </div><span>protocol Protocol<br></span><span>{<br></span><span> typealias Argument<br></span><span> <br></span><span> func function()<br></span><span> func function(_: Argument)<br></span><div><div><span>}<br></span><span> <br></span><div><div><blockquote type="cite"><div>On 16-Jan-2016, at 3:29 PM, 肇鑫 <<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I wonder where is the good for a protocol designer on this?<br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">zhaoxin</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 16, 2016 at 5:23 PM, Vatsal Manot via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Currently, the following the code fails with multiple errors:<br>
<br>
protocol Protocol<br>
{<br>
typealias Argument<br>
<br>
func function(argument: Argument = default)<br>
}<br>
<br>
I propose that we allow protocols to require functions with default parameter values. I can’t see any disadvantages to this, and the change would only be additive.<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><br>Owen Zhao<br></div></div></div>
</div></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div><div dir="ltr"><div><br>Owen Zhao<br></div></div></div>
</div>
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>