<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="">You need to know that the NAME of a method or function isn't just outside the parenthesis.<div class=""><br class=""></div><div class="">The name of </div><div class=""><br class=""></div><div class=""> func IsTextEmpty(foo : String?) -> Bool? <br class=""><br class=""></div><div class="">is</div><div class=""><br class=""></div><div class=""> "IsTextEmpty(foo:)"</div><div class=""><br class=""></div><div class="">and the name of</div><div class=""><br class=""></div><div class=""><br class=""> func IsTextEmpty(text : String?) -> Bool<br class=""><br class=""></div><div class="">is</div><div class=""><br class=""></div><div class=""> "IsTextEmpty(text:)"</div><div class=""><br class=""></div><div class="">The argument labels are important.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> IsTextEmpty(foo : <span style="color: #703daa" class="">String</span>?) -> <span style="color: #703daa" class="">Bool</span>? {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">return</span> foo?.<span style="color: #703daa" class="">isEmpty</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> IsTextEmpty(text : <span style="color: #703daa" class="">String</span>?) -> <span style="color: #703daa" class="">Bool</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">guard</span> <span style="color: #ba2da2" class="">let</span> text = text <span style="color: #ba2da2" class="">else</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class=""> </span>return<span style="color: #000000" class=""> </span>true</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> }</div><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255); min-height: 16px;" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""> <span style="color: #ba2da2" class="">return</span> text.<span style="color: #703daa" class="">isEmpty</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93); background-color: rgb(255, 255, 255);" class=""><span style="color: #3e1e81" class="">print</span><span style="color: #000000" class="">(</span><span style="text-decoration: underline" class="">I</span>sTextEmpty<span style="color: #000000" class="">(foo: </span><span style="color: #d12f1b" class="">"1"</span><span style="color: #000000" class="">), </span>IsTextEmpty<span style="color: #000000" class="">(text: </span><span style="color: #d12f1b" class="">"2"</span><span style="color: #000000" class="">))</span></div></div><div class=""><font face="Menlo" style="font-size: 14px;" class=""><span style="color: #000000" class="">// prints </span><b class="">Optional(false) false</b></font></div><div class=""><b style="font-family: Menlo; font-size: 14px;" class=""><br class=""></b></div><div class=""><br class=""><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">--<br class="">C. Keith Ray<br class="">Senior Software Engineer / Trainer / Agile Coach<br class="">* <a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf" class="">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a><br class=""></div><div style="color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div></div><br class="Apple-interchange-newline">
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Oct 10, 2017, at 10:27 AM, C. Keith Ray <<a href="mailto:keithray@mac.com" class="">keithray@mac.com</a>> 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=""><font face="Menlo" style="font-size: 14px;" class="">nope. it's the same as this example:</font><div class=""><font face="Menlo" style="font-size: 14px;" class=""><br class=""></font></div><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> funny() -> <span style="color: #703daa" class="">Int</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="" class=""> </span>return<span style="" class=""> </span><span style="color: #272ad8" class="">1</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> funny() -> <span style="color: #703daa" class="">String</span> {</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="" class=""> </span>return<span style="" class=""> </span><span style="color: #d12f1b" class="">"2"</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">print(funny()) // the compiler doesn't know which one you want.</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">// the above doesn't compile.</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">// <b class="">error: forloop.playground:8:1: error: ambiguous use of 'funny()'</b></div><div class=""><b class=""><br class=""></b></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class="">You have to have some difference visible in the caller:</div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div class=""><br class=""></div></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: #ba2da2" class="">func</span> funny(useInt: <span style="color: #703daa" class="">Bool</span>) -> <span style="color: #703daa" class="">Int</span> {</div><div style="margin: 0px; line-height: normal; color: rgb(186, 45, 162);" class=""><span style="" class=""> </span>return<span style="" class=""> </span><span style="color: #272ad8" class="">1</span></div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: #ba2da2" class="">func</span> funny(useString: <span style="color: #703daa" class="">Bool</span>) -> <span style="color: #703daa" class="">String</span> {</div><div style="margin: 0px; line-height: normal; color: rgb(186, 45, 162);" class=""><span style="" class=""> </span>return<span style="" class=""> </span><span style="color: #d12f1b" class="">"2"</span></div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: #3e1e81" class="">print</span>(<span style="color: #31595d" class="">funny</span>(useInt: <span style="color: #ba2da2" class="">true</span>), <span style="color: #31595d" class="">funny</span>(useString: <span style="color: #ba2da2" class="">true</span>))</div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">// prints "1 2\n"</div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">--<br class="">C. Keith Ray<br class="">Senior Software Engineer / Trainer / Agile Coach<br class="">* <a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf" class="">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a><br class=""></div><div style="font-family: 'Lucida Grande'; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: LucidaGrande;" class="">* </span><a href="https://leanpub.com/wepntk" style="font-family: LucidaGrande;" class="">https://leanpub.com/wepntk</a><span style="font-family: LucidaGrande;" class=""> <- buy my book?</span><br style="font-family: LucidaGrande;" class=""></div></div><br class="Apple-interchange-newline">
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 10, 2017, at 9:06 AM, Phil Kirby via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class="">
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class="">
<table class="">
<tbody class="">
<tr class="">
<td class="votecell">
<div class="vote"><a class="vote-up-off" title="This question shows research effort; it is useful and clear"></a> <span itemprop="upvoteCount" class="vote-count-post">2</span> <a class="vote-down-off" title="This question does not show any research effort; it is unclear or not useful">down vote</a> <a class="star-off" href="https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type#" title="Click to mark as favorite question (click again to undo)">favorite</a>
<div class="favoritecount"><b class=""></b></div>
</div>
</td>
<td class="postcell">
<div class="">
<div class="post-text" itemprop="text"><p class="">Original StackOverflow post:</p><p class=""><a href="https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type" class="">https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type</a><br class=""></p><p class="">I was under the impression that swift can have overloaded methods that differ only in the type of object that the methods return. I would think that I could have two funcs with the same signature yet they differ in return type.</p>
<pre class="lang-swift prettyprint prettyprinted"><code class=""><span class="kwd">import</span><span class="pln"> </span><span class="typ">Foundation</span><span class="pln">
</span><span class="com">// ambiguous use of 'IsTextEmpty(text:)'
</span><span class="kwd">func</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text </span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?)</span><span class="pln"> </span><span class="pun">-></span><span class="pln"> </span><span class="typ">Bool</span><span class="pun">?</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> text</span><span class="pun">?.</span><span class="pln">isEmpty
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">func</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text </span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?)</span><span class="pln"> </span><span class="pun">-></span><span class="pln"> </span><span class="typ">Bool</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">guard</span><span class="pln"> </span><span class="kwd">let</span><span class="pln"> text </span><span class="pun">=</span><span class="pln"> text </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">true</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> text</span><span class="pun">.</span><span class="pln">isEmpty
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">let</span><span class="pln"> text</span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">nil</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">let</span><span class="pln"> empty </span><span class="pun">=</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text</span><span class="pun">:</span><span class="str">"text"</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
print</span><span class="pun">(</span><span class="str">"Not Empty"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text</span><span class="pun">:</span><span class="pln"> text</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
print</span><span class="pun">(</span><span class="str">"Empty"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">}</span></code></pre><p class="">Here, both functions have the same input parameters but one func returns an optional <code class="">Bool?</code> and the other returns a <code class="">Bool</code>. In this case I get an error:</p>
<pre class="lang-swift prettyprint prettyprinted"><code class=""><span class="pln">ambiguous use of '</span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text</span><span class="pun">:)</span><span class="pln">'</span></code></pre><p class="">If I change the name of one of the input parameters I no longer get the ambiguous error:</p>
<pre class="lang-swift prettyprint prettyprinted"><code class=""><span class="com">// Works
</span><span class="kwd">func</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">foo </span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?)</span><span class="pln"> </span><span class="pun">-></span><span class="pln"> </span><span class="typ">Bool</span><span class="pun">?</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> foo</span><span class="pun">?.</span><span class="pln">isEmpty
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">func</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text </span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?)</span><span class="pln"> </span><span class="pun">-></span><span class="pln"> </span><span class="typ">Bool</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">guard</span><span class="pln"> </span><span class="kwd">let</span><span class="pln"> text </span><span class="pun">=</span><span class="pln"> text </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">true</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">return</span><span class="pln"> text</span><span class="pun">.</span><span class="pln">isEmpty
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">let</span><span class="pln"> text</span><span class="pun">:</span><span class="pln"> </span><span class="typ">String</span><span class="pun">?</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">nil</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="kwd">let</span><span class="pln"> empty </span><span class="pun">=</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">:</span><span class="str">"text"</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
print</span><span class="pun">(</span><span class="str">"Not Empty"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="typ">IsTextEmpty</span><span class="pun">(</span><span class="pln">text</span><span class="pun">:</span><span class="pln"> text</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
print</span><span class="pun">(</span><span class="str">"Empty"</span><span class="pun">)</span><span class="pln">
</span><span class="pun">}</span></code></pre><p class="">Shouldn't the compiler detect that they are two distinct methods even though their return types are different, since an optional <code class="">Bool?</code> is a different type from a non-optional <code class="">Bool</code>?</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;" class=""><br class="">
<div class=""></div>
</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=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>