<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 30, 2016, at 5:02 AM, Toni Suter via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I am trying to get a better understanding of Swift's function overload resolution rules.</div><div class="">As far as I can tell, if there are multiple candidates for a function call, Swift favors</div><div class="">functions for which the least amount of parameters have been ignored / defaulted. For example:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">// Example 1</font></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int) {&nbsp; print("f1") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int, y: Int = 0) { print("f2") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f(x: 0) <span class="Apple-tab-span" style="white-space:pre">        </span>// f1</font></div></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">// Example 2</font></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int, y: Int = 0) {&nbsp; print("f1") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int, y: Int = 0, z: Int = 0) { print("f2") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f(x: 0) <span class="Apple-tab-span" style="white-space:pre">        </span>// f1</font></div></div><div class=""><br class=""></div><div class="">It also looks like Swift favors functions with default-value parameters over functions with variadic parameters:</div><div class=""><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int = 0) {&nbsp; print("f1") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">func f(x: Int...) { print("f2") }</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f() <span class="Apple-tab-span" style="white-space:pre">                        </span>// f1</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f(x: 1) <span class="Apple-tab-span" style="white-space:pre">                </span>// f1</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f(x: 1, 2) <span class="Apple-tab-span" style="white-space:pre">                </span>// f2 (makes sense because f1 would not work here)</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class="">f(x: 1, 2, 3) <span class="Apple-tab-span" style="white-space:pre">                </span>// f2&nbsp;</font><span style="font-family: Menlo;" class="">(makes sense because f1 would not work here)</span></div></div><div class=""><br class=""></div><div class="">But then I tested functions with default-value parameters and variadic parameters and things start to get weird.</div><div class="">For example, this must be a bug, right?</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class="">func f(x: Int..., y: Int = 0) { print(x, y) }</font></div><div class=""><font face="Menlo" class="">func f(x: Int...) { print(x) }</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">f()<span class="Apple-tab-span" style="white-space:pre">                        </span>// []</font></div><div class=""><font face="Menlo" class="">f(x: 1)<span class="Apple-tab-span" style="white-space:pre">                        </span>// [1]</font></div><div class=""><font face="Menlo" class="">f(x: 1, 2)<span class="Apple-tab-span" style="white-space:pre">                </span>// [1, 2] 0</font></div><div class=""><font face="Menlo" class="">f(x: 1, 2, 3)<span class="Apple-tab-span" style="white-space:pre">                </span>// [1, 2, 3]</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">I think, in this example, it should always call the second overload, because</div><div class="">no parameter is ignored / defaulted. What do you think?</div></div></div></blockquote><div><br class=""></div>Can you open a new bug report for this at <a href="http://bugs.swift.org" class="">bugs.swift.org</a>?</div><div><br class=""></div><div>Mark</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">Thanks and best regards,</div><div class="">Toni</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>