Skip to content

Instantly share code, notes, and snippets.

@dahu
Created April 15, 2015 22:51
Show Gist options
  • Select an option

  • Save dahu/6a55e42b38b42f9df254 to your computer and use it in GitHub Desktop.

Select an option

Save dahu/6a55e42b38b42f9df254 to your computer and use it in GitHub Desktop.
Vim's copy() vs deepcopy()
" 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