# Pitch: didSet Semantics

**URL:** <https://forums.swift.org/t/pitch-didset-semantics/27858>\
**Category:** Pitches\
**Tags:** observer, didset, property\
**Created:** [August 13, 2019, 6:17pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858 "2019-08-13T18:17:10Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [August 28, 2019, 8:10pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/61 "2019-08-28T20:10:09Z")

</div>

Has anyone considered making "does not take the oldvalue" behavior explicit? This seems like a power user feature, and making it explicit in code is good.

For example, it could be as a simple as `didset() {` where the `()` indicate that oldvalue isn't taken.

Edit: Yes this was discussed upthread. This seems like the way to go to me. If we did this, there is a simple and clean way to handle this:

The compiler emits a warning "oldValue not used" for a didSet without an `(oldValue)` specifier. It would produce two notes along the lines of:

1. "insert () to explicitly turn off oldvalue"
2. "insert (oldvalue) to explicitly take and ignore it.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 28, 2019, 8:14pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/62 "2019-08-28T20:14:00Z")

</div>

Well, we can propose eliminating the implicit parameter, but I think that will be much more controversial.

Should we also eliminate the implicit parameter from `willSet`? The parallelism argument is strong, and it would have some concrete advantages — for example, we could trigger a parameterless `willSet` before an entire read-write operation instead of at the end just before setting, which might allow the modification to happen in-place.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 28, 2019, 8:20pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/63 "2019-08-28T20:20:17Z")

</div>

> [@John\_McCall](#):
>
> Should we also eliminate the implicit parameter from `willSet` ? The parallelism argument is strong, and it would have some concrete advantages — for example, we could trigger a parameterless `willSet` before an entire read-write operation instead of at the end just before setting, which might allow the modification to happen in-place.

That would be fantastic, in my view.

Any source-breaking change would (and _should_) be controversial but I think being able to reap concrete benefits, while allowing the user a clear (and, ideally as here, automatable) upgrade path is the strongest argument for such a change.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 28, 2019, 8:25pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/64 "2019-08-28T20:25:59Z")

</div>

> [@Chris\_Lattner3](#):
>
> The compiler emits a warning "oldValue not used" for a didSet without an `(oldValue)` specifier.

So `didSet`, alone among the accessors, just has to be given this empty parameter clause forever because we got the semantics wrong for awhile?

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [August 28, 2019, 8:26pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/65 "2019-08-28T20:26:20Z")

</div>

It feels like this pitch started with an idea for a relatively minor optimization and is trending toward a source-breaking change that would make these kinds of handlers more cumbersome to write. I can understand how a change like this would lead to a more rigorous language, but I don't see what problem it's solving beyond the initial, very limited-in-scope optimization.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 28, 2019, 8:34pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/66 "2019-08-28T20:34:19Z")

</div>

Well, it's not just an optimization because (1) reading from the old value can be semantically problematic and (2) if we can enable in-place modification, the "optimization" can eliminate linear costs and thus unblock the use of observers for certain kinds of work. But yes, there is a substantial breadth-of-scope inflation going on here.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 28, 2019, 8:40pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/68 "2019-08-28T20:40:42Z")

</div>

> [@TypeSwiftApostle](#):
>
> Wouldn't the expected form be `didSet(_)` , like it is done in pattern matching ?
> 
> I'm personally in favour of the following:
> 
> - `didSet(_)` when ignoring `oldValue` ,

That is how you would express performing a read for its side effects while ignoring the result; it can’t mean _not_ performing a read.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [August 28, 2019, 8:49pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/69 "2019-08-28T20:49:50Z")

</div>

> [@John\_McCall](#):
>
> So `didSet` , alone among the accessors, just has to be given this empty parameter clause forever because we got the semantics wrong for awhile?

I don't understand the undercurrent of your question. Are you saying that `didset()` or `didset(oldvalue)` is onerous to write or difficult to understand?

Accessors aren't that common and are not important to sugar at this level (imo) given the semantic issues that sugar raises. I'd rather have the behavior be explicit - this makes it easier to reason about.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 28, 2019, 9:07pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/71 "2019-08-28T21:07:57Z")

</div>

It's inconsistent that `didSet` would be required to have a parameter clause, even when not taking any parameters, when no other accessors are. Imagine teaching this to a newcomer: how would you explain that you can write `get` and `set` and `willSet`, but you can't just write `didSet`?

Here's how I think about it. Your proposed warning practically forces programmers using `oldValue` in `didSet` to change their code in a future release of the Swift tools. I don't see the benefit of doing that over making it mandatory, but conditional on a future compatibility version (i.e. it's an error to use an implicit `oldValue` in Swift 6+ mode; that's an easy special-case diagnostic/fixit/migration). Once we're doing that, we might as well leave the people who _aren't_ using `oldValue` alone.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [August 29, 2019, 1:21am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/72 "2019-08-29T01:21:46Z")

</div>

Do we have an idea what fraction of `didSet` implementations do and do not use `oldValue`?

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 29, 2019, 1:38am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/73 "2019-08-29T01:38:17Z")

</div>

Quite a lot of them don’t use `oldValue`: [Sign in to GitHub · GitHub](https://github.com/search?utf8=%E2%9C%93&q=didSet+NOT+oldValue+language%3ASwift&type=Code)

A lot of code uses `didSet` to trigger some other work and doesn’t really care about the `oldValue`.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 29, 2019, 1:41am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/74 "2019-08-29T01:41:38Z")

</div>

400K+ results on GitHub where `didSet` doesn’t has a reference to `oldValue`.

30K+ results on GitHub where `didSet` has a reference to `oldValue`.

---

<div class="post-metadata">

**Author:** ![hisekaldma](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hisekaldma/32/1146_2.png) [@hisekaldma](https://forums.swift.org/u/hisekaldma)\
**Post date:** [August 29, 2019, 7:40am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/75 "2019-08-29T07:40:08Z")

</div>

> [@suyashsrijan](#):
>
> 400K+ results on GitHub where `didSet` doesn’t has a reference to `oldValue` .
> 
> 30K+ results on GitHub where `didSet` has a reference to `oldValue` .

Looks like I’m wrong about what the common case is.

I still think requiring `didSet(oldValue)` would be an overall usability and teachability regression. For the language to make sense as a whole, we’d have to require `willSet(newValue)` too. Requiring `set(newValue)` is clearly out of the question.

So then we’d end up in the situation that `willSet` and `didSet` require writing out the parameter if you need it, but not `set`. Maybe that’s an okay place to draw the line – `willSet` and `didSet` are definitely more advanced features than `get` and `set`. But to a newcomer, the distinction would feel arbitrary and pointless. Imagine someone just learning to program:

– ”Why can't I just write `didSet` when I can write `set`?”  
– ”Because otherwise there would be an implicit read that might have side effects.”  
– ”Come again?”

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [August 29, 2019, 8:28am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/76 "2019-08-29T08:28:42Z")

</div>

> [@hisekaldma](#):
>
> Requiring `set(newValue)` is clearly out of the question.

Why?

---

<div class="post-metadata">

**Author:** ![hisekaldma](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hisekaldma/32/1146_2.png) [@hisekaldma](https://forums.swift.org/u/hisekaldma)\
**Post date:** [August 29, 2019, 10:22am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/77 "2019-08-29T10:22:32Z")

</div>

> [@xwu](#):
>
> Why?

I don't see how the source breakage could possibly be justified. Practically every use of `set` references `newValue`. That's very different from the situation with `didSet` and `willSet`.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [August 29, 2019, 11:44am UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/78 "2019-08-29T11:44:18Z")

</div>

I skipped a few last posts but will study them in a moment.

Maybe this idea is completely wrong, so don‘t judge me too hard on this.

Could accessors act syntactically like functions with a trailing closure? (This would however imply the deprecation of `didSet(oldValue)`, `set(newValue)` etc.)

Instead we would either have a closure parameter list or not.

```swift
didSet { oldValue in
  ...
}

```

If the user used an implicit `oldValue` the compiler would raise a warning and provide a fixit to add a missing parameter list.

* * *

This is just a syntactic alternative. As I mentioned, I‘m not sure it will solve any of the issues mentioned upthread, but I wanted to throw this into the bucket of proposed options.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [August 29, 2019, 3:07pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/79 "2019-08-29T15:07:33Z")

</div>

I don’t see how that achieves anything.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [August 29, 2019, 3:27pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/80 "2019-08-29T15:27:46Z")

</div>

> [@suyashsrijan](#):
>
> 400K+ results on GitHub where `didSet` doesn’t has a reference to `oldValue` .
> 
> 30K+ results on GitHub where `didSet` has a reference to `oldValue` .

Wow...that's much more heavily weighted than I expected.

In that case this change will not only fix a few bugs, but will also have a meaningful positive performance impact in aggregate.

To avoid a positive change getting caught up in tangential BikeShedding gridlock, IMO this should go through with no syntax changes. If/when someone wants to put a proposal together to re-evaluate the syntax for PropertyAccessors/PropertyObservers, (is there a single name for them?), it should be considered separately.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [August 29, 2019, 8:40pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/81 "2019-08-29T20:40:36Z")

</div>

Yes. The scope of the proposal is **well-defined** and a change to the syntax was _intentionally_ put in the **Future Directions** section, because it _needs_ its own pitch and proposal where we can have a more in-depth discussion about other accessors and implicit arguments. This proposal is purely about semantics and changes to the syntax are clearly out of scope.

I don't know when the Core Team plans to run a review for this, but if at the end of the review they decide that we must make the deprecation now, then I can update the implementation to do so (it would only requires a few trivial changes). However, it's not something that I am officially proposing, so I would leave the decision to them. If someone wants to discuss the syntax changes further (and take it forward) then I encourage them to create a new pitch thread.

---

<div class="post-metadata">

**Author:** ![drewbenson](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@drewbenson](https://forums.swift.org/u/drewbenson)\
**Post date:** [September 14, 2019, 9:37pm UTC](https://forums.swift.org/t/pitch-didset-semantics/27858/82 "2019-09-14T21:37:01Z")

</div>

> [@daveanderson](#):
>
> I agree with this ^ statement that the use of `oldValue` without explicitly writing `didSet(oldValue)` should be deprecated. IMO it is overly "magic" for the getter to be called iff `oldValue` is referenced (and not calling the getter if it is not).

I'm generally not in favor of anything that makes me do more work than what I'm already used to doing. However, in the case of **oldValue** in didSet or **error** in catch, when I was new to Swift, it was _ **very** _ counterintuitive that these magic variables would just appear in some cases.

Similarly, the **$0** and **$1** in closures made me ask, "what the heck is this, some shell script?". Even now, they seem ugly and out-of-place in Swift, a language that ostensibly prefers clarify over brevity. If we can reference parameters by number in a closure, why not in a regular function?

[Previous page](https://forums.swift.org/t/pitch-didset-semantics/27858.md?page=3)

[Next page](https://forums.swift.org/t/pitch-didset-semantics/27858.md?page=5)
