[Pitch] Weak let

As I explained in the original discussion, my only remaining concern (at the time) was whether "updating" the weak let capture when the original instance goes out of scope is thread-safe or not.

I am happy to hear that this is the case! So I don't see any problem with having weak let and give a (probably unqualified) +1.

As a matter of fact, these discussions show that perhaps the resulting mental model of forbidding it is not correct. I believe I even remember a student of mine being perplexed a bit, along the lines of: "Okay, it can become nil, but I am not the one mutating it?" (When explaining the syntax and reasoning behind it in the context of reference cycles I did use the, I assume, standard explanation of "it has to be a var because it can become nil")

Btw, the same can indeed be said by requiring var for read-only computed properties. On the one hand they are "variable" indeed as two subsequent accesses could give you "variable" results, but newcomers are sometimes thrown off because in spite of seeing the var keyword they cannot assign values to it. No personal anecdotes here, though (perhaps because this is usually explained a lot sooner when students just soak everything up without asking too many questions... maybe I need to slow down at the beginning...). :smiley:

1 Like

Scanning the codebase I'm working with using these regexps:

\[.*weak ([a-zA-Z0-9_]+)\b.*\]\s*in.*?(?:\n.*?){,500}?(?<!let )\1\s*=(?!=)
\[.*weak ([a-zA-Z0-9_]+)\b.*\]\s*in.*?(?:\n.*?){,500}?&\1

0 results, out of 3224 weak captures.

For open-source search using SourceGraph, I cannot use capture groups, so I've focused on the weak self:

  • self = - no examples
  • &self - 1 example, probably a bug

Is there a better way of doing it? E.g. some script that would check out a list of projects and try to build them with modified compiler.

It's an experimental feature, not the upcoming one. You are right, the proposal is to flip this unconditionally. My understanding was that it is specified in the text to let people know what feature to enable when testing the toolchain. If that's not a correct usage of the proposal template, I can remove it, or change wording to "Experimental Feature Flag".

1 Like

This would greatly help adopt strict concurrency.

I didn't see it explicitly in the proposal text, but like weak var, all weak lets must also be optional?

3 Likes

Yes. I’ll update the proposal text.

1 Like

3 posts were merged into an existing topic: SE-0481: weak let

This proposal is now under review as SE-0481. I’ve moved a recent question to the review thread to centralize discussion, and I’m closing this thread.