plans for macros
Steven Schveighoffer
schveiguy at yahoo.com
Wed May 14 08:31:25 PDT 2008
I just found a very good use for macros, and I was wondering how they could
be used to help in this situation.
If I have a log object, and that log object is supposed to evaluate its
arguments only if the logging level allows it, checked at runtime.
So this is the ideal usage in the calling function:
if(log.isEnabledAtLevel(Information))
log.output(someExpensiveStringBuild());
This is ideal because it only outputs at the appropriate level, and it only
evaluates the expensive function if the log level is enabled.
However, this is very verbose, and is prone to errors. Many log systems use
the following method:
log.outputInformation(someExpensiveStringBuild());
Which does the if-statement for you. However, they warn you to write your
logging code in the first form if the code to build the output is expensive
to avoid building the output even when it is not output. But D has a better
way:
class Log
{
void outputInformation(lazy string x)
{
if(isEnabledAtLevel(Information))
output(x);
}
}
Now, we can still use the second form, even when building the string is
expensive. But there are issues with this solution. For one, lazy
evaluation adds delegate functions wherever the logging is required, adding
to runtime and code bloat. Second, variadic functions would be nice for
logging, especially with formatting, but the only way to do lazy variadic
functions is with template tuples, so there is another lot of generated
code, and is even more inefficient.
But a macro would solve the problem quite nicely. A macro would evaluate
the if statement in the calling function, and so would prevent evaluation of
the expensive string building unless necessary, AND would require no
delegates to do it.
The question I have is, when macros are implemented, can I have a 'class
scoped' macro? That is, a macro that knows what context it is supposed to
be in, and is passed a 'this' pointer? And will macros support variadic
arguments?
For example, I'd like to have a macro to output formatted log information
only if the log is enabled, but I want to call it like a member function of
the log.
-Steve
More information about the Digitalmars-d-learn
mailing list