Multiple distinct objects: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring, made p2js compatible
(Added solution for Action!)
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
Line 1,172:
=={{header|Phix}}==
Phix uses shared reference counts with copy-on-write semantics. Creating n references to the same mutable object is in fact the norm,
but does not cause any of the issues implicitly feared in the task description. In fact, apart from dictionaries and classes as noted below, it is not possible to create shared references such that when one is updated they all are, instead store an index to another table that stores the object, rather than the object itself. Also, apart from low-level trickery and interfacing to shared libraries, there are no pointers to normal hll objects. Sequences need not be homogeneous, they can contain any type-mix of elements.
 
such that when one is updated they all are, instead store an index to another table that stores the object, rather than the object itself. Also, apart from low-level trickery and interfacing to shared libraries, there are no pointers to normal hll objects. Sequences need not be homogeneous, they can contain any type-mix of elements.
However, JavaScript uses pass-by-sharing semantics, so if (and only if) we specify "with javascript_semantics" (or just "with js" for short) the last line triggers a "p2js violation" error on desktop/Phix, indicating it must be changed (as shown).
<lang Phix>sequence s = repeat("x",3*rand(3))
<!--<lang Phix>(phixonline)-->
?s
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
s[rand(length(s))] = 5
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"x"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
?s
<span style="color: #000000;">s</span><span style="color: #0000FF;">[$]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
s[rand(length(s))] &= 'y'
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'y'</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
?s
<span style="color: #000080;font-style:italic;">-- s[2] = s ?s</span>
s[rand(length(s))] = s
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
?s<!--</lang>-->
{{out}}
<pre>
Line 1,189 ⟶ 1,190:
{"xy",{"xy","x","x","x","x",5},"x","x","x",5}
</pre>
Note that the last statement did not create a circular structure, something that is not possible in Phix, except (perhaps) via index-emulation.<br>
I suppose it is possible that someone could write
<!--<lang Phix>sequence s = repeat(my_func(phixonline),5)</lang-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">my_func</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
end for<!--</lang>-->
and expect my_func() to be invoked 5 times, but for that you need a loop
<!--<lang Phix>sequence s = repeat(0,5phixonline)-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
s[i] = my_func()
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">my_func</span><span style="color: #0000FF;">()</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
There are in fact two "reference" types in phix: dictionaries and classes, which are created using the function calls new_dict() and new() respectively.<br>
In the same manner as above if you want five distinct dictionaries or classes, you must invoke new_dict()/new() five times.
 
=={{header|PicoLisp}}==
7,796

edits