====== Dependent templates ======
We've already seen how template parameters can be used to construct [[intro_example:chp_fifo|parameterized circuits]], as well as change the size of arrays in the [[intro_example:templates|process port list]]. In addition, they can also be used to specify parameters used in the template itself.
template
defproc test(chan!(int) out)
{
chp {
(; i : N : out!data[i])
}
}
In the example above, the process ''test'' takes a parameter ''N'', followed by a parameter array of size ''N''.
pint x[3];
x[0] = 1; x[1] = 3; x[2] = 5;
test<3,x> t;
In the above example, the CHP body for instance ''t'' would be expanded into
out!1; out!3; out!5
Dependent templates have //left to right// evaluation order. In the example above this means that ''t.N'' is created and assigned value ''3'', and then after that the value of ''t.N'' can be used to infer the type of ''pint d[N]''. Switching the order would result in an error:
template defproc test(chan!(int) out);
-[ERROR]-> The identifier `N' does not exist in the current scope
Parameters can be used in expressions as well. For example, the memory definition in the [[stdlib:start|ACT standard library]] is:
export template
defproc ram (chan?(int<2>) rd; chan?(int) addr;
chan?(int) din; chan!(int) dout)
{
...
}
The first template parameter ''N'' corresponds to the number of elements in the memory, and ''W'' corresponds to the bit-width of the data in the memory. The bit-width of the address channel ''addr'' is computed from ''N'' by a standard library parameter function that is used to compute the smallest number of address bits needed to be able to hold the address values.