Compilation does not terminate for recursive function

I'm porting some C++ with templates, and I realize that there is no such thing as if constexpr in mojo. Im trying to write something similar to the following test. However, compilation (not surprisingly) does not terminate. Question: how do other people handle these straightforward recursive functions?
fn test[N: Int]() -> InlinedFixedVector[Int, 1<<N]:
if N == 0:
var result = InlinedFixedVector[Int, 1<<N](1<<N)
result[0] = 0
return result
else:
alias N2: Int = N-1
let sub: InlinedFixedVector[Int, 1<<N2] = test[N2]()
let subsize = 1<<N2

var result = InlinedFixedVector[Int, 1<<N](1<<N)
for i in range(subsize):
result[i] = sub[i]
result[i + subsize] = sub[i] + subsize
return result
fn test[N: Int]() -> InlinedFixedVector[Int, 1<<N]:
if N == 0:
var result = InlinedFixedVector[Int, 1<<N](1<<N)
result[0] = 0
return result
else:
alias N2: Int = N-1
let sub: InlinedFixedVector[Int, 1<<N2] = test[N2]()
let subsize = 1<<N2

var result = InlinedFixedVector[Int, 1<<N](1<<N)
for i in range(subsize):
result[i] = sub[i]
result[i + subsize] = sub[i] + subsize
return result
2 Replies
Alex Kirchhoff
Alex Kirchhoff12mo ago
I believe @parameter if in Mojo is quite similar to if constexpr in C++ -- does that work?
Henk-Jan Lebbink
Henk-Jan LebbinkOP12mo ago
@Alex Kirchhoff Thank you, Yes that solves my issues. I missed that somewhat important line in the doc.
Want results from more Discord servers?
Add your server