[swift-evolution] Proposal: Re-instate mandatory self for accessing instance properties and functions
spestov at apple.com
Tue Dec 15 16:43:55 CST 2015
> On Dec 15, 2015, at 2:39 PM, James Campbell <james at supmenow.com> wrote:
> I think this should be extended to functions as well. How would you know the difference between a global function and a class function ?
We could take this to its logical extreme and require explicit capture lists for all closures, and some kind of prefix (outer.) on captured variable accesses inside closure bodies. :-)
However, nobody would seriously propose this these days, because prevailing wisdom is that functions should be short and easy to read at a glance, especially if they involved nested functions, making it clear what values are captured where and so on.
Why don't we push the same philosophy for classes also?
I'm not just convinced that syntactic disambiguation of scope lookup is desirable...
> On Tue, Dec 15, 2015 at 10:28 PM, Slava Pestov via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Hi all,
> I don't see anyone lay out how this proposal can interact with nested types and such. There's a fair amount of complexity in Swift with what you can do by nesting things inside of each other, so the simple conceptual model of "locals are unqualified, instance variables have a self. prefix" doesn't seem to generalize.
> Will I need to qualify associated types with the protocol or type name to refer to them? What about generic type parameters, they're sort of like "instance variables" too.
> What about class methods that want to call each other? Do they need the explicit 'self', or an explicit class name prefix? The latter changes semantics if the class method is overridden in a subclass.
> If we ever add the ability for an inner type to capture stored properties from the outer type, how do you reference properties of the outer type? I guess the problem has to be solved anyway to refer to an outer "self" explicitly, but qualifying everything with OuterType.self.foo kind of defeats the purpose of inner types altogether.
> I'm not sure I buy the readability arguments in favor of this approach. It seems the languages where explicit 'self' was adopted did it mostly by accident, or because of implementation concerns. In Python for instance, there's no way for assignment to modify a binding in an outer scope, so 'foo = bar' always sets a local named 'foo', IIRC. So explicit self is needed there. Greg Parker explains earlier in this thread by explicit self was chosen for Objective-C, and it wasn't readability.
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> james at supmenow.com <mailto:james at supmenow.com>
> +44 7523 279 698
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution