<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=""><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="color: #000000" class=""> </span>return<span style="color: #000000" 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="color: #000000" class=""> </span>return<span style="color: #000000" 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="color: #000000" class=""> </span>return<span style="color: #000000" 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="color: #000000" class=""> </span>return<span style="color: #000000" 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="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;"><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><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="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>