Jump to content

Parametric polymorphism: Difference between revisions

Added C3 example
(Added a procedure "initTree", a procedure "map", a procedure "print" and an example.)
(Added C3 example)
Line 219:
if (right != NULL)
right->replace_all (new_value);
}</lang>
 
=={{header|C3}}==
 
<lang c3>public template tree <Type>
{
public struct Tree
{
Type value;
Tree* left;
Tree* right;
}
 
public func void Tree.replaceAll(Tree *a_tree, Type new_value)
{
a_tree.value = new_value;
if (a_tree.left) a_tree.left.replaceAll(new_value);
if (a_tree.right) a_tree.right.replaceAll(new_value);
}
}</lang>
 
To use a parametric type, the type must be defined first:
 
<lang c3>define IntTree = tree<int>::Tree;
 
func void test()
{
IntTree inttree;
inttree.replaceAll(3);
}</lang>
 
38

edits

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