There are 2 semantics in Nim and low-level programming languages like C, C++, 
Rust.

1\. Value semantics.

2\. Reference semantics.

Value semantics means that the copy owns its memory and lives separately from 
the copied.

Reference semantics means that the copy and the copied refer to the same 
underlying memory location.

Now:

  * raw objects have value semantics: on assignment you get a full copy. On 
deepCopy you get a full copy and on shallowCopy you also get a full copy. They 
are also called trivial types or POD for Plain Old Data.



Now you might want to now why shallowCopy actually deep copies, well that's 
because they are values they don't refer to any memory location so you can't 
just copy the reference.

  * ref objects have reference semantics. When you do an assignment you 
actually copy the reference to a memory location. A shallow copy does the same, 
a deepCopy instead will recursively create a new memory location and copy the 
memory referred to to that location.
  * sequence and strings have value semantics. They copy on assignments. This 
avoids many errors where you copy an objects, modify it and use the old one and 
don't realize that it has been modified. Now internally they are implemented 
with a reference to a resizable and movable memory location. And copying that 
is quite costly so Nim exposes the internal details via shallowCopy when 
avoiding such a copy is desirable.
  * int, bool, enum are like objects, trivial types/POD.


Reply via email to