<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">
<table>
<tbody>
<tr>
<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></b></div>
</div>
</td>
<td class="postcell">
<div>
<div class="post-text" itemprop="text">
<p>Original StackOverflow post:</p>
<p><a href="https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type">https://stackoverflow.com/questions/46620311/overloading-methods-where-only-difference-is-optional-vs-non-optional-type</a><br /></p>
<p>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><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>Here, both functions have the same input parameters but one func returns an optional <code>Bool?</code> and the other returns a <code>Bool</code>. In this case I get an error:</p>
<pre class="lang-swift prettyprint prettyprinted"><code><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>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><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>Shouldn't the compiler detect that they are two distinct methods even though their return types are different, since an optional <code>Bool?</code> is a different type from a non-optional <code>Bool</code>?</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;"><br />
<div></div>
</div>
</body>
</html>