Parametric polymorphism: Difference between revisions

Updated C3
m (syntax highlighting fixup automation)
(Updated C3)
Line 222:
 
=={{header|C3}}==
<syntaxhighlight lang="c3">module tree(<Type>);
 
struct Tree
Line 231:
}
 
fn void Tree.replaceAllreplace_all(Tree* a_tree&self, Type new_value)
{
a_treeself.value = new_value;
if (a_treeself.left) a_treeself.left.replaceAllreplace_all(new_value);
if (a_treeself.right) a_treeself.right.replaceAllreplace_all(new_value);
}
</syntaxhighlight>
 
Usage:
To use a parametric type, the type must be defined first:
<syntaxhighlight lang="c3">define IntTree =import tree<int>::Tree;
 
<syntaxhighlight lang="c3">define IntTree = tree<int>::Tree;
 
fn void test()
{
IntTreeTree(<int>) inttree;
inttree.replaceAllreplace_all(3);
}</syntaxhighlight>
 
38

edits