2 ms·
The article mentions shallow copy, but does this create a persistent immutable data structure? Does it modify all nodes up the tree to the root?
by jbritton 8mo ago
The article mentions shallow copy, but does this create a persistent immutable data structure? Does it modify all nodes up the tree to the root?
- jcparkyn 8mo agoYes, if you modify a nested dict/list entry, all nodes above it will be cloned. Here's an example: x = [1, [2]]; var y = x; set y.[0] = 3; // clones the outer array, keeps a reference to inner array set y.[1].[0] = 4; // clones the inner array here. Outer array is now exclusive so it doesn't need another clone. var z = x; set z.[1].[0] = 4; // clones both arrays at once