Just one time
Andrea Fontana via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Oct 20 08:48:46 PDT 2015
It happens I need to perform an operation just one time (inside a
function, a loop...)
I wonder if doing this it's a good idea or not.
bool isFirstTime(alias T)()
{
static val = true;
if (val)
{
val = false;
return true;
}
return false;
}
So:
void my_func()
{
if (isFirstTime!my_func) writeln("First call!");
else writeln("Just another call");
}
myfunc(); // Print First call!
myfunc(); // Print Just another call;
Or (for example inside a for loop):
assert(isFirstTime!"blah" == true);
assert(isFirstTime!"blah" == false);
Does it work as expected?
More information about the Digitalmars-d-learn
mailing list