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?

Reply via email to