Parametric polymorphism: Difference between revisions

Content added Content deleted
m (Sorry I meant C# not C++!)
Line 717: Line 717:
{5,6,7,"apples","oranges"}
{5,6,7,"apples","oranges"}
</pre>
</pre>
For comparison, this is the D example from this page translated to Phix.<br>
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
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
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
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
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
tree, just by changing that top-level call to object root2 = tmap(root,rid).
object root2 = tmap(root,routine_id("add10")).
<lang Phix>enum data, left, right
<lang Phix>enum data, left, right


Line 742: Line 741:
object root = newnode(1.00)
object root = newnode(1.00)
-- Add some nodes.
-- Add some nodes.
root[left] = newnode(1.10);
root[left] = newnode(1.10)
root[left][left] = newnode(1.11);
root[left][left] = newnode(1.11)
root[left][right] = newnode(1.12);
root[left][right] = newnode(1.12)
root[right] = newnode(1.20);
root[right] = newnode(1.20)
root[right][left] = newnode(1.21);
root[right][left] = newnode(1.21)
root[right][right] = newnode(1.22);
root[right][right] = newnode(1.22)
-- Now the tree has seven nodes.
-- Now the tree has seven nodes.
-- Show the arrays of the whole tree.
-- Show the whole tree.
ppOpt({pp_Nest,2})
ppOpt({pp_Nest,2})
pp(root)
pp(root)
-- Modify the arrays of the whole tree.
-- Modify the whole tree.
root = tmap(root,routine_id("add10"))
root = tmap(root,routine_id("add10"))


-- Show the arrays of the whole tree again.
-- Show the whole tree again.
pp(root)
pp(root)
end procedure
end procedure