Jump to content

Parametric polymorphism: Difference between revisions

m
Sorry I meant C# not C++!
m (Sorry I meant C# not C++!)
Line 717:
{5,6,7,"apples","oranges"}
</pre>
For comparison, purposes (and because this entry looked a bit sparse without it) this is the D example from this page translated to Phix.<br>
Note that tmap has to be a function rather than a procedure with a reference parameter, but this still achieves
pass-by-reference/in-situ updates, mainly because root is a local rather than global/static, and is the target
of (aka assigned to/overwritten on return from) the top-level tmap() call, and yet it would also manage the
C++#/Dart/Kotlin thing (by which I am referring to those specific examples on this page) of creating a whole new
tree, just by changing that one top-level call to object root2 = tmap(root,rid).
object root2 = tmap(root,routine_id("add10")).
<lang Phix>enum data, left, right
 
Line 742 ⟶ 741:
object root = newnode(1.00)
-- Add some nodes.
root[left] = newnode(1.10);
root[left][left] = newnode(1.11);
root[left][right] = newnode(1.12);
root[right] = newnode(1.20);
root[right][left] = newnode(1.21);
root[right][right] = newnode(1.22);
-- Now the tree has seven nodes.
-- Show the arrays of the whole tree.
ppOpt({pp_Nest,2})
pp(root)
-- Modify the arrays of the whole tree.
root = tmap(root,routine_id("add10"))
 
-- Show the arrays of the whole tree again.
pp(root)
end procedure
7,806

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.