<div dir="ltr"><div>> But implicit self is confusing in a lot of code</div><div><br></div><div>On the other hand, it allows a logical explanation of how you can take code from global scope and put it into an instance scope:</div><div><br></div><div>let greeting = "Hello"</div><div>let name = "Michael"</div><div><br></div><div>func greet() {</div><div> print("\(greeting), \(name)")</div><div>}</div><div><br></div><div>seemlessly becomes</div><div><br></div><div>class Greeter {</div><div><br></div><div> let greeting = "Hello"</div><div> let name = "Michael"</div><div><br></div><div> func greet() {</div><div> print("\(greeting), \(name)")</div><div> }</div><div><br></div><div>}</div><div><br></div><div>> can (and does) lead to shadowing bugs,</div><div><br></div><div>There are simple strategies that help to minimize the amount of shadowing, e.g. </div><div><br></div><div>- only expose the minimum necessary amount of names in any scope </div><div>- break functions into small part so that it's easy to see all the local name declarations</div><div>- not use any globals, or at least name them in a visually different way (UppercaseCamelStyle)</div><div>- name properties and locals in a different way (classProperty, local_var)</div><div><br></div><div>Even without a formal code style, if you tend to make property names longer and local names shorter, your risk of shadowing goes down.</div><div><br></div><div>> .x and .f() to mark implicit self. I realize that this may conflict with enum usage.</div><div><br></div><div>This will lead to a lot of ambiguity:</div><div><br></div><div>func f() {</div><div> let x = NSOperation()</div><div> .name = "Name" // is it <a href="http://x.name">x.name</a> or <a href="http://self.name">self.name</a>??</div><div> ...</div><div>}</div><div><br></div><div>> If so, then use another marker. For instance :x or ^x or anything.</div><div><br></div><div>This is workable, but still I think this is one of the best points of Swift – the existence of instance scope where names are simply written as-is. This helps implementing patterns like "take a long function and make it into a struct with a bunch of small functions instead".</div><div><br></div><div>> is very difficult to reason about in diffs or any other interface that isn't an IDE (especially code review)</div><div><br></div><div>This is the point where I entirely agree, good code should be easily read in any context.</div><div>Again, may I suggest you take a look into using a style guide to differentiate visually between local and instance scope?</div><div><br></div><div>Ilya</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Dec 13, 2015 at 10:15 AM, Rob Napier via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I wanted to reopen this discussion that seems to have trailed off. Requesting the return of self was my very first ask of Swift if I remember correctly (<a href="https://devforums.apple.com/message/1013085" target="_blank">https://devforums.apple.com/message/1013085</a>). Continued work in Swift has both strengthened and modified that ask. Here are several of the examples discussed before:<div><br></div><div><div><a href="https://gist.github.com/schwa/94b11dc0a7a331f46b25" target="_blank">https://gist.github.com/schwa/94b11dc0a7a331f46b25</a></div><div><a href="https://gist.github.com/rnapier/478465d1b15e95b98b42" target="_blank">https://gist.github.com/rnapier/478465d1b15e95b98b42</a></div><div><a href="https://gist.github.com/rnapier/4213dc64206b17df6935" target="_blank">https://gist.github.com/rnapier/4213dc64206b17df6935</a></div><div><a href="https://gist.github.com/dwineman/d6c56ec0c0e2fdb761db" target="_blank">https://gist.github.com/dwineman/d6c56ec0c0e2fdb761db</a></div><div><div><br></div><div>I get that it seems tedious to type (and read) "self." and I get that "self." is currently a hint that self might be captured (but doesn't actually mean that, since you can use self. without capturing, and sometimes have to, very often in init, so really it's basically meaningless for that use).</div><div><br></div><div>That's why I suggest using .x and .f() to mark implicit self. I realize that this may conflict with enum usage. If so, then use another marker. For instance :x or ^x or anything. But implicit self is confusing in a lot of code, can (and does) lead to shadowing bugs, and is very difficult to reason about in diffs or any other interface that isn't an IDE (especially code review).</div><div><br></div><div>Thoughts, David? I agree with your basic proposal; I just want to amend it.</div><div><br></div><div>-Rob</div></div></div><div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1p9Jer2O6jVE9KWvo-2B9iUaEyN8slp4IizyiLwsfp54PB6QfGHj8FHyiXAU-2BmFfS5mBpWjgxHgjUn6uFtGMhTY5T-2BUIfoxsGQ80Lfgxke88uoXc4nBl6ZM-2FXTHfDd3Se1pAYlk9wJ2gf64FM2FWnfnPm5oEOz-2F-2Bs-2B5ramOTdQYSZyWTSJ-2B81ZnB-2Bgg8-2BSW2uCIQsXIj-2Bb49b6i4dwDm-2F0geafX9DTnN2FJbMtkHrptIM-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
<br></blockquote></div><br></div>