plans for macros
boyd
gaboonviper at gmx.net
Thu May 15 13:21:49 PDT 2008
On Thu, 15 May 2008 21:18:06 +0200, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> "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
--------
I see your point. Yeah, I think I'll definately turn my Log function into
a macro when they become part of D.
There is some information on macros in the presentation of the
D-conference 2007.
http://s3.amazonaws.com/dconf2007/WalterAndrei.pdf
It says there that names can be looked up in context of the macro
definition. It doesn't say if that scope can actually be an instance of a
class though. I guess this question can only be answered by the D language
developers.
There is also something about function declarations acting like class
members. So a function:
void inc(ref int a)
{
a++;
}
could be called like this:
a.inc();
Again there is no telling whether this will go for macros as well, but I
think the chances that it eventually will are positive. It's the kind of
syntactic sugar similar to other stuff in D that Walter seems to like.
And otherwise you'll be stuck with Log(MyLog312, "logging stuff"), which
is not that bad either.
Cheers,
Boyd
More information about the Digitalmars-d-learn
mailing list