I’m playing with dotty macros and want to do a kind of crappy loop unrolling. So given an expression, run it N times by repeating the expression rather than using a loop. I’m very close.
I have the hardest time figuring out which parts of an inline function happen at compile time and which happen at runtime too.
You can get the constant value from some expression with .valueOrError, but I’m not sure how that will help you, or how to get a quoted parameter from a normal parameter, and what their difference is exactly, or how to get an expression from a quoted paramter.
Well, from the error message, it seems I want n to be a literal Int, but it doesn’t tell me how to do that. I’m hoping someone can tell me because I’m having a heck of a time figuring it out on my own.
At compile time, I need n to be known, and then runNImpl will generate code that repeats fn times.
I suppose this doesn’t answer the question directly, but an alternative to a macro might be inline match.
inline def runN(inline n: Int)(inline arg: Unit): Unit =
inline n match {
case 0 => ()
case i if i > 0 =>
arg
runN(i - 1)(arg)
case i if i < 0 => compiletime.error(compiletime.codeOf(n) + " is negative")
case _ => compiletime.error(compiletime.codeOf(n) + " is not a literal")
}