<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="">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's a scope where you have *global* symbols to the file - i.e. they should be available *globally* anywhere within the file.<div class=""><br class=""></div><div class="">I undestand that this goes against the overall hierarchy of shadowing, though, which is why I've proposed an alternative: the compiler shouldn'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're overloading a method.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Jun 20, 2016, at 8:59 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">I can see how this is a problem, but I don'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're proposing.<br class=""><br class="">I'd rather cope temporarily with the current limitation, which as you mentioned has a workaround everywhere except in Playgrounds; in Playgrounds, there isn't any reason why you couldn't just rename one or the other.<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Jun 20, 2016 at 13:55 Charlie Monroe via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></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" class="">I'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" class="">https://bugs.swift.org/browse/SR-1772</a>).<div class=""><br class=""></div><div class="">Motivation:</div><div class=""><br class=""></div><div class="">Consider the following code:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">private</span><span class=""> </span><span style="color:#bb2ca2" class="">func</span><span class=""> _validateAccount(name: </span><span style="color:#703daa" class="">String</span><span class="">, existingAccounts: [</span><span style="color:#703daa" class="">Account</span><span class="">]) -> </span><span style="color:#703daa" class="">Bool</span><span class=""> {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class=""> <font color="#008400" class="">// Logic goes here...</font></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo;min-height:10px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class="">class</span><span class=""> MyController {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""> private</span><span class=""> </span><span style="color:#bb2ca2" class="">func</span><span class=""> _validateAccount(name: </span><span style="color:#703daa" class="">String</span><span class="">) -> </span><span style="color:#703daa" class="">Bool</span><span class=""> {</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""> </span> <span style="color:rgb(187,44,162)" class="">let</span><span class=""> accounts = self.accounts</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class=""><br class=""></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class=""><span style="white-space:pre-wrap" class="">        </span><font color="#008400" class="">// Error: Extra argument 'existingAccounts' in call</font></span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span style="color:#bb2ca2" class=""> </span> <span style="color:rgb(187,44,162)" class="">return</span><span class=""> _validateAccount(name, existingAccounts: accounts)</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class=""> }</span></div><div style="margin:0px;font-size:9px;line-height:normal;font-family:Menlo" class=""><span class="">}</span></div><div class=""><span class=""><br class=""></span></div><br class="">_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 class=""><br class=""></div><div class="">Currently, the compiler gives an error about ambiguous use of _validateAccount, since it'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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">This is partially an issue of the ongoing discussion of various name collisions between modules, but in this particular case, it's one single file.</div><div class=""><br class=""></div><div class=""><div class="">I propose that file-level symbols take precedence over any reference to current type'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't a function satisfying the arguments.</div></div><div class=""><br class=""></div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div>
</div></blockquote></div><br class=""></div></div></body></html>