Don't copy it, share it
transcript
What if you didn't copy at all? There is another way. Make the items immutable. They can never change.
A shared item is only dangerous if something can change it. That's the whole risk. A frozen item can't change, so sharing it harms nobody. It also costs nothing.
Both carts hold the same pizza object. Nobody changed it. Only the cola is replaced, with a new item for three colas. The past cart still holds its own cola, at two.
The cart holds 2 items, a pizza and a cola. Only 1 of them was replaced. And 1 is shared. Nobody can change it, so it's safe.
The past order still says 380. The receipt is safe. No deep copy. No clone. Just a changed copy of the cart.
Python's replace does it in one call. So does a Java wither, or a JavaScript spread. Each makes a changed copy.
Full breakdown in the lecture.