Every once in a while I come across the notion that R has copy-on-modify semantics, for example in Hadley's devtools wiki.
Most R objects have copy-on-modify semantics, so modifying a function argument does not change the original value
I can trace this term back to the R-Help mailing list. For example, Peter Dalgaard wrote in July 2003:
R is a functional language, with lazy evaluation and weak dynamic typing (a variable can change type at will: a <- 1 ; a <- "a" is allowed). Semantically, everything is copy-on-modify although some optimization tricks are used in the implementation to avoid the worst inefficiencies.
Similarly, Peter Dalgaard wrote in Jan 2004:
R has copy-on-modify semantics (in principle and sometimes in practice) so once part of an object changes, you may have to look in new places for anything that contained it, including possibly the object itself.
Even further back, in Feb 2000 Ross Ihaka said:
We put quite a bit of work into making this happen. I would describe the semantics as "copy on modify (if necessary)". Copying is done only when objects are modified. The (if necessary) part means that if we can prove that the modification cannot change any non-local variables then we just go ahead and modify without copying.
It's not in the manual
No matter how hard I've searched, I can't find a reference to "copy-on-modify" in the R manuals, neither in R Language Definition nor in R Internals
Question
My question has two parts:
- Where is this formally documented?
- How does copy-on-modify work?
For example, is it proper to talk about "pass-by-reference", since a promise gets passed to the function?