26: Compile‐time Loops - royal-lang/rl GitHub Wiki
Just like Royal has compile-time conditionals and it also supports compile-time loops.
All type of loops like for,foreach and while have static versions.
A static loop can be executed anywhere and not just in code and can be used to dynamically create multiple variables etc.
Static loops do not create scopes BUT any compile-time constants like enum created within their scope will be unique per iteration and thus there is a compile-time scope.
static for (PRE_INITIALIZATION, CONDITION, POST_EXPRESSION) // Or just: static for PRE_INITIALIZATION, CONDITION, POST_EXPRESSION
{
...
}
static foreach (VALUE, ITERATABLE_ELEMENTS) // ITERATABLE_ELEMENTS are ex. arrays, ranges etc. Or just: foreach VALUE, ITERATABLE_ELEMENTS
{
...
}
static foreach (INDEX, FROM .. TO) // FROM and TO must be integers.
{
...
}
static foreach (KEY,VALUE, ITERABLE_ELEMENTS) // ITERABLE_ELEMENTS are ex. arrays, associative arrays etc.
{
...
}
static while (CONDITION) // Or just: static while CONDITION
{
...
}
static do
{
...
} static while (CONDITION);
Examples:
enum names = ["x", "y", "z"];
struct Point3d
{
static foreach (name, names)
{
enum varName = "var int " ~ name ~ ";";
mixin(varName);
}
}
Result:
struct Point3d
{
var int x;
var int y;
var int z;
}