<div dir="ltr">On Mon, Jun 20, 2016 at 2:17 PM, Charlie Monroe <span dir="ltr">&lt;<a href="mailto:charlie@charliemonroe.net" target="_blank">charlie@charliemonroe.net</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">IMHO file-level scope *is* special. Aside from the fact that some declarations (e.g. protocols) are only valid at file-level scope, the way I see it, it&#39;s a scope where you have *global* symbols to the file - i.e. they should be available *globally* anywhere within the file.<div><br></div><div>I undestand that this goes against the overall hierarchy of shadowing, though, which is why I&#39;ve proposed an alternative: the compiler shouldn&#39;t (in case of invoking methods) just check the base name, but it should do a more thorough search, looking at the arguments as well. Just like when you&#39;re overloading a method.</div></div></blockquote><div><br></div><div>I could support a proposal for that alternative.</div><div> </div><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><div class="h5"><div><br></div><div><br></div><div><div><div><blockquote type="cite"><div>On Jun 20, 2016, at 8:59 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br><div>I can see how this is a problem, but I don&#39;t like this solution for the same reasoning that Jordan provided in the bug. Namely, the general rule is that the inner scope shadows the outer scope. This would be a very strange exception that you&#39;re proposing.<br><br>I&#39;d rather cope temporarily with the current limitation, which as you mentioned has a workaround everywhere except in Playgrounds; in Playgrounds, there isn&#39;t any reason why you couldn&#39;t just rename one or the other.<br><div class="gmail_quote"><div dir="ltr">On Mon, Jun 20, 2016 at 13:55 Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I&#39;ve filed this previously as a compiler bug, but was told to discuss this here (<a href="https://bugs.swift.org/browse/SR-1772" target="_blank">https://bugs.swift.org/browse/SR-1772</a>).<div><br></div><div>Motivation:</div><div><br></div><div>Consider the following code:</div><div><br></div><div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">private</span><span> </span><span style="color:#bb2ca2">func</span><span> _validateAccount(name: </span><span style="color:#703daa">String</span><span>, existingAccounts: [</span><span style="color:#703daa">Account</span><span>]) -&gt; </span><span style="color:#703daa">Bool</span><span> {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span>    <font color="#008400">// Logic goes here...</font></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo;min-height:10px"><span></span><br></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">class</span><span> MyController {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">    private</span><span> </span><span style="color:#bb2ca2">func</span><span> _validateAccount(name: </span><span style="color:#703daa">String</span><span>) -&gt; </span><span style="color:#703daa">Bool</span><span> {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">    </span>    <span style="color:rgb(187,44,162)">let</span><span> accounts = self.accounts</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span><br></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span><span style="white-space:pre-wrap">        </span><font color="#008400">// Error: Extra argument &#39;existingAccounts&#39; in call</font></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">    </span>    <span style="color:rgb(187,44,162)">return</span><span> _validateAccount(name, existingAccounts: accounts)</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo"><span>}</span></div><div><span><br></span></div><br>_validateAccount(name:, existingAccounts:) is declared at file-level since it is used in two separate controllers (my example comes from an app with accounts and the check is done during creation + when renaming) and then a similar method (with less arguments) is declared on the controllers.</div><div><br></div><div>Currently, the compiler gives an error about ambiguous use of _validateAccount, since it&#39;s declared on MyController as well which is taking precedence, even though the number of arguments is different (and the compiler is only checking base names, not arguments).</div><div><br></div><div>As Jordan Rose mentioned in a comment to my report, solution to this is to specify the function using Module._validateAccount, but this is not applicable to Playgrounds.</div><div><br></div><div>This is partially an issue of the ongoing discussion of various name collisions between modules, but in this particular case, it&#39;s one single file.</div><div><br></div><div><div>I propose that file-level symbols take precedence over any reference to current type&#39;s members, unless explicit self is specified, or at least when it comes to calling a method, the type checker goes up the scope hierarchy to see if there isn&#39;t a function satisfying the arguments.</div></div><div><br></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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>
</div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div></div>