[swift-evolution] Proposal: Re-instate mandatory self for accessing instance properties and functions

Rob Napier robnapier at gmail.com
Mon Dec 14 13:01:11 CST 2015

On Mon, Dec 14, 2015 at 10:32 AM, Dave Abrahams via swift-evolution <
swift-evolution at swift.org> wrote:

> > On Dec 14, 2015, at 7:29 AM, Jeremy Pereira via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> >>
> >> On 14 Dec 2015, at 09:01, Andrew Brown via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>
> >> This discussion isn't going to be solved quickly so +1 for a compiler
> flag.
> >> I'd propose the flag generates an error if member variables are
> accessed without self.
> >
> > I’d propose it only be a warning. The reason for this is that Chris
> Lattner, in an unrelated thread, expressed an aversion to compiler flags
> that create different dialects of the language and I agree with him.
> Please consider that individually-selectable warnings have a tendency to
> do the same thing.
Agreed. This should not be a warning. There should be a correct way to
reference global functions, constants, variables, methods, and properties.
If leading "self." or punctuation is recommended, it should be required. If
scopes should have no distinction, then adding unnecessary "self" should be
discouraged at a minimum, not encouraged with an optional warning.

I prefer a distinction for methods and properties (specifically I prefer
punctuation), but I currently drop all unnecessary "self." because
consistency is more critical than preference, and Apple's sample code
doesn't use "self." except where required. Adding warnings makes it very
hard to share code that was written by different teams with different
opinions. It is better to bend than splinter.

But I do find that the current system (where a property looks like a local
variable and a method looks just like a global function) makes code review
harder in the tools that code review is usually done with (which are web
browsers, email and chat sessions, not editors or IDEs). Since I believe
that code review is second only to strong types in reducing bugs, I want
code review to be as easy as possible. So I advocate a distinction, and
think punctuation (particularly :x) is the lightest-weight solution.

But please, no warnings. There should be a single approach for stdlib, and
that approach should be what we all use.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151214/35098992/attachment.html>

More information about the swift-evolution mailing list