Created
April 15, 2015 22:51
-
-
Save dahu/6a55e42b38b42f9df254 to your computer and use it in GitHub Desktop.
Vim's copy() vs deepcopy()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " Barry Arthur, April 2015 | |
| " | |
| " copy() vs deepcopy() | |
| " | |
| " copy() - a gets an independent copy of x | |
| " but b gets a copy of the reference to y | |
| " so changes made to b are reflected in y. | |
| let x = {'foo' : 1} | |
| let y = {'bar' : x} | |
| let a = copy(x) | |
| let b = copy(y) | |
| let a.foo = 2 | |
| let b.bar.foo = 2 | |
| echo a.foo b.bar.foo x.foo y.bar | |
| " deepcopy() - b gets its own independent | |
| " copy of y, including an independent copy | |
| " of the x within it. | |
| let x = {'foo' : 1} | |
| let y = {'bar' : x} | |
| let a = deepcopy(x) | |
| let b = deepcopy(y) | |
| let a.foo = 2 | |
| let b.bar.foo = 2 | |
| echo a.foo b.bar.foo x.foo y.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment