How do we have function on metaprogramming to get a value from an input value, just like C++ equivalent one e.g.
template<int N>
constexpr int fac()
{
return N∗fac<N−1>();
}
template<>
constexpr int fac<1>() {
return 1;
}
constexpr int x5 = fac<5>();
as on Nim it's macro, thanks in advance
Wrap the code in a static: code block. It will execute it at compile time. Example:
import sugar
let nums = static:
collect newSeq:
for i in 0 .. 10: i
echo nums
Don't know how is metaprogramming relevant to your question.
func fac(n: int): int {.compileTime.} = # pragma is necessary if you'd like to limit function's use to CT
if n == 0: 1
else: n * fac(n - 1)
const f = fac(5)
static:
echo "Welcome from compile time!"
doAssert f == 120
proc fac[N: static[int]](): int =
when N == 1:
1
else:
N * fac[N - 1]()
const x5 = fac[5]()