plans for macros
Steven Schveighoffer
schveiguy at yahoo.com
Thu May 15 12:18:06 PDT 2008
"boyd" wrote
> What about just adding an if statement-within this kind of template?
> Wouldn't that suffice?
>
> Using the ',' instead of concatenation can prevent a lot of complicated
> string functions. The only difference between that and macro's would be
> the function call and putting the objects on the stack. Basically the only
> advantage of macros would be that the function call is inlined.
>
> Cheers,
> Boyd
That helps, but the arguments are still evaluated even if the if statement
is false.
For example:
int logLevel;
void Log(T...)(T t)
{
if(logLevel > 3)
writefln(t);
}
class C
{
char[] toString() { return "This " ~ " is a C"; }
}
void main()
{
logLevel = 2;
auto c = new C;
Log(c);
}
In this case, the concatenation done in C.toString() is still evaluated,
even though we aren't going to use it. That is what the lazy delegates
helps with. You can 'fix' this by doing:
void Log(T...)(lazy T t) {/*same implementation*/}
And now, if the log level is <= 3, the delegates are never called, and no
expensive concatenation is done when it won't be used.
But that means that each argument has a delegate created for it, plus you
are generating lots of extra template functions, when the macro way of
building the code into the call site would generate almost no extra code,
and would need no delegates. Not to mention the issue with static strings
and IFTI.
I do not deny that the problem is solvable with current language features.
However, it is more correctly and *efficiently* solved with macros. I'm
just trying to find out if the syntax will be palatable.
-Steve
More information about the Digitalmars-d-learn
mailing list