Rely on @Published Property Wrapper Events, Not the Observed Object

Pop quiz!

What is the output of this program, e.g. when run in a playground or an autoclosure block?

import Combine

class ValueHolder {
    @Published var value: Int = 0
    init() {}

let valueHolder = ValueHolder()
let sub = valueHolder.$value.sink { value in
    print("received", value, "- persisted", valueHolder.value)
valueHolder.value = 4
valueHolder.value = 2
Spoiler: Solution
received 0 - persisted 0
received 4 - persisted 0
received 2 - persisted 4

The documentation indicates as much, but it can still be a surprise in your programs when you treat the @Published event as a mere signal to read multiple values from the observed object.

So remember that @Published publishes events in its willSet.