I'm trying to create a macro that expands an incrementing value into the output every time it is called. So far I've got this, but it re-initializes the value to 0 every time:
macro test1 {
case { _ $x
} => {
var n = n ? n+1 : 0;
letstx $n = [makeValue(n,#{here})];
return #{
$n;
}
}
}
test1 ()
test1 ()
yields:
0;
0;
when what I want is:
0;
1;
How do I define n
to be a global variable, so that I can increment and retain its value outside the macro?
Update:
I can get it to work by changing the assignment to n
to an eval, but this really feels like cheating:
var n = eval("if (typeof n != 'undefined') n++; else n=0; n")
At the moment (there are plans make this better) you can just use the global object:
macro test1 {
case { _ $x
} => {
window.n = typeof window.n !== 'undefined' ? window.n+1 : 0;
letstx $n = [makeValue(n,#{here})];
return #{
$n;
}
}
}
test1 ()
test1 ()