Re:Copying data is wasteful, mutating data is dangerous
by gowld on Friday January 10, 2020 @09:44AM
(#22011229)
For the read case, do you mean that in nonbranching code, a read can be considered a destroy+create that cancel out?
How is that legal if the reads are also writes into other variables, or copied into an array/tuple and returned to a caller? That seems a clear violation of linearity.
Also, that doesn't seem true for branching structures, but I guess technically in an immutable language loops iterations are recursive functions.
On the surface that can seem a bit stupid, but the practical outcome is that compilers can determine when an array's contents can be mutated in place rather than overwritten.
A variable you read over and over can be thought of theoretically as one you duplicate, then read one copy of. Since the act of reading the copy destroys it, a compiler implements this by merely reading the value directly.
Passing an array into a subroutine however means the compiler either copies it, or forbids further access after passing it in. If it's the latter, the memory space is available for re-use inside the subroutine.
Rust uses affine type theory for its borrow system to provide memory safety. Function language research is looking at linear and affine types for a variety of applications including performance.