Parametric polymorphism: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
(Improved D entry) |
||
Line 189: | Line 189: | ||
=={{header|D}}== |
=={{header|D}}== |
||
D |
D templates can parametric by constant too. |
||
<lang d> |
<lang d>class ArrayTree(T, int LEN) { |
||
⚫ | |||
ArrayTree left, right; |
|||
⚫ | |||
class ArrayTree(T, int LEN) { |
|||
// template instantiation in alias |
|||
⚫ | |||
const int length = LEN ; |
|||
⚫ | |||
ANode LHS, RHS ; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}</lang> |
|||
This is a using example. |
|||
<lang d>module polytest ; |
|||
import std.stdio ; |
|||
import parapoly ; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
alias ArrayTree!(real, 3) VecT ; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
void main() { |
void main() { // demo code |
||
import std.stdio; |
|||
⚫ | |||
v.LHS = new VecT(1.11) ; v.LHS.LHS = new VecT(1.11) ; v.LHS.RHS = new VecT(1.12) ; |
|||
v.RHS = new VecT(1.21) ; v.RHS.LHS = new VecT(1.21) ; v.RHS.RHS = new VecT(1.22) ; |
|||
⚫ | |||
// do something start from root v |
|||
// whose length(LEN) is 3. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// create the tree root |
|||
writefln() ; |
|||
auto root = new VecT(0.01); |
|||
⚫ | |||
⚫ | |||
root.left = new VecT(1.11); |
|||
root.left.left = new VecT(1.11); |
|||
root.left.right = new VecT(1.12); |
|||
root.right = new VecT(1.21); |
|||
root.right.left = new VecT(1.21); |
|||
root.right.right = new VecT(1.22); |
|||
// now the tree has seven nodes |
|||
// show arrays of the whole tree |
|||
⚫ | |||
// change the arrays of the whole tree |
|||
⚫ | |||
⚫ | |||
// show arrays of the whole tree again |
|||
writeln(); |
|||
⚫ | |||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre>[0.01, 0.01, 0.01] |
|||
[1.11, 1.11, 1.11] |
|||
[1.11, 1.11, 1.11] |
|||
[1.12, 1.12, 1.12] |
|||
[1.21, 1.21, 1.21] |
|||
[1.21, 1.21, 1.21] |
|||
[1.22, 1.22, 1.22] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9] |
|||
[0.9, 0.9, 0.9]</pre> |
|||
=={{header|E}}== |
=={{header|E}}== |