Deepcopy: Difference between revisions

462 bytes added ,  2 years ago
→‎{{header|Phix}}: explained "with js" and that deep_copy() is now a builtin
(Deepcopy en FreeBASIC)
(→‎{{header|Phix}}: explained "with js" and that deep_copy() is now a builtin)
Line 1,846:
=={{header|Phix}}==
{{libheader|Phix/basics}}
Handled natively. Phix uses reference counting with copy-on-write semantics; the initial copy is fast even for huge complex and deeply nested structures (copying a single machine-word-sized reference and incrementing a single machine-word-sized reference count), and when a shared object (anything with a refcount>1) is modified, an internal clone of the minimum necessary levels occurs, with all the rest of the structure remaining shared, (but obviously still properly protected in the same way).
<!--<lang Phix>-->
<span style="color: #004080;">object</span> <span style="color: #000000;">a<span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span>
Line 1,862:
It is worth noting that this mechanism is also used for parameter passing, and when a local variable is both passed as a parameter and assigned on return, automatic pass-by-reference handling kicks in to avoid any unnecessary internal cloning.
 
However, JavaScript has pass-by-sharing semantics, and since 1.0.0 when pwa/p2js (the Phix to JavaScript transpiler) was introduced, a simple "with js" (which works identically to "with javascript_semantics") effectively disables '''both''' underlying semantic mechanisms simultaneously, and causes desktop/Phix to raise fatal runtime errors ("p2js violation: relies on copy on write semantics") indicating where a deep_copy() is needed, which is now a builtin (see builtins\repeat.e for the actual) and is a bit like this:
Should you for some (probably misguided) reason really want it, a recursive clone function might look like this:
<!--<lang Phix>-->
<span style="color: #008080;">function</span> <span style="color: #000000;">deep_copy<span style="color: #0000FF;">(<span style="color: #004080;">object</span> <span style="color: #000000;">o<span style="color: #0000FF;">)</span>
Line 1,880:
<span style="color: #0000FF;">?<span style="color: #000000;">c
<!--</lang>-->
Or you ''could'' just serialize and deserialize, though it would probably be quite a bit slower:
<!--<lang Phix>-->
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #000000;">serialize<span style="color: #0000FF;">.<span style="color: #000000;">e</span>
7,804

edits