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

Nick Shelley nickmshelley at gmail.com
Mon Dec 7 11:47:23 CST 2015


David,

I disagree that your two examples don't tell us much.

foobar({
print(self.description)
})

This tells us that self will be captured by the closure, and reminds us
that if that might create a retain cycle, to avoid memory leaks we need to
add [weak self] or [unowned self] at the beginning of the closure.

barfoo({
print(description)
})

This tells us that we don't need to worry about self being captured in the
closure because the compiler would force us to add self if anything that
caused it to be captured were used in the closure.

This is very helpful information to me, and information that would stand
out less if I got used to seeing self everywhere, which is what your
proposal would force.

On Sun, Dec 6, 2015 at 6:10 PM, David Hart <david at hartbit.com> wrote:

> Hi Nick,
>
> I understand the quote "This makes the capturing semantics of self stand
> out more in closures”, but this is a very weak statement in Swift for me.
> Let me try to explain.
>
> If we use the try keyword as an example:
>
> try foobar()
> barfoo()
>
> If the previous lines of code compile without error, we know without a
> shadow of a doubt that foobar is a throwing function and that barfoo does
> not throw. The compiler will not compile the first line without the keyword
> and would not allow it in on the second line.
>
> Now if we go back to the example of self in closures:
>
> foobar({
> print(self.description)
> })
>
> The self keyword in the previous lines of code does not tell us anything
> at all:
>
>
>    - self might have been forced by the compiler to warn us.
>    - self might have been a programmer choice if the closure was
>    non-escaping.
>
>
> And the reverse:
>
> barfoo({
> print(description)
> })
>
> This also does not tell us much:
>
>
>    - The closure might be non-escaping.
>    - description might be referring to a local variable (which we missed
>    the declaration) shadowing the instance property in an escaping closure.
>
>
> In both of these last examples, we can’t tell by having a quick look at
> the code at the point of call if we should really be careful about memory
> or not.
>
> With the proposition, self gets some meaning back: it indicates which are
> local and which are instance properties.
>
> David.
>
>
> On 06 Dec 2015, at 23:55, Nick Shelley via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> I like that self is only required in closures because it serves as a good
> reminder that there are memory and safety implications with using self in a
> closure, such as creating retain cycles or having the closure run after
> self has been deallocated.
>
> I can't seem to find an official Apple Swift style guide, but github's (
> https://github.com/github/swift-style-guide) suggests only using self in
> closures with the rationale: "This makes the capturing semantics of self
> stand out more in closures, and avoids verbosity elsewhere."
>
> On Sat, Dec 5, 2015 at 3:16 AM, Yichen Cao <ycao at me.com> wrote:
>
>> Teaching wise, its much less confusing for self to be required so
>> students don't mix up instance properties and local vars. Especially when
>> self is required in closures, it confuses students. If self is mandatory
>> for all instance properties, it would be so much clearer and much easier to
>> read.
>>
>> Yichen
>>
>> On Dec 5, 2015, at 18:11, swift-evolution-request at swift.org wrote:
>>
>> Re: Proposal: Re-instate mandatory self for accessing
>>      instance properties and functions (David Hart)
>>
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/6f0bc185/attachment.html>


More information about the swift-evolution mailing list