|
Post by Jeroen P. Broks on Aug 31, 2019 12:29:55 GMT 1
NIL has support for static locals variables. They work similar to how they work in C.
Example in C:
#include <stdio.h>
void Test(){ static int statcount = 0; int nonstatcount = 0; statcount++; nonstatcount++; printf("%d\t%d",statcount,nonstatcount); }
int main(){ Test(); Test(); Test(); Test(); Test(); return 0; }
If you write and compile this in C, you will see two columns of numbers. The left one counts up, and the right one always displays 1. Because non-static locals always reset when a new function call instance starts, while statics do not.
This is now also possible in NIL
void Test() static int statcount = 0 int nonstatcount = 0 statcount++ nonstatcount++ print(statcount,nonstatcount) end
Test() Test() Test() Test() Test()
This will cause the same effect. One word of caution is in order... static variables are slower than normal local variables. In normal usage you will not notice any difference, but if speed is essential (in which case I wouldn't recommend a scripting language in general), you gotta keep it in mind.
|
|