Parametric polymorphism: Difference between revisions
Content added Content deleted
(Added a procedure "initTree", a procedure "map", a procedure "print" and an example.) |
(Added C3 example) |
||
Line 219: | Line 219: | ||
if (right != NULL) |
if (right != NULL) |
||
right->replace_all (new_value); |
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> |
}</lang> |
||