Thread overview
C#'s conditional attributes
Jul 12, 2010
Walter Bright
July 12, 2010
Just saw this on reddit:

http://www.reddit.com/r/programming/comments/cocww/little_known_c_feature_conditional_attributes/

and was wondering whether this can be done in D by relying on lazy parameters and the inliner. Here's the test bed:

void log(A...)(lazy string format, lazy A objects)
{
}

void main(string args[])
{
    log(args[0] ~ "yah", args ~ args, 42 + args.length);
}

The dependency on args is intended to prevent the optimizer from hoisting evaluation to compilation time.

In an ideal world, the compiler should generate lambdas for the three expressions, pass them to log(), then inline, figure out there's no really use of either lambda, and let them vanish.

Indeed that's quite what happens when compiling with -O -inline -release. The generated _Dmain is the same as a baseline with an empty main(). Once this is in place, a simple version() selector dispatches to either the empty log() above or one that actually logs stuff. Useful to know.


Andrei
July 12, 2010
Andrei Alexandrescu wrote:
> Just saw this on reddit:
> 
> http://www.reddit.com/r/programming/comments/cocww/little_known_c_feature_conditional_attributes/ 
> 
> 
> and was wondering whether this can be done in D by relying on lazy parameters and the inliner. Here's the test bed:
> 
> void log(A...)(lazy string format, lazy A objects)
> {
> }
> 
> void main(string args[])
> {
>     log(args[0] ~ "yah", args ~ args, 42 + args.length);
> }
> 
> The dependency on args is intended to prevent the optimizer from hoisting evaluation to compilation time.
> 
> In an ideal world, the compiler should generate lambdas for the three expressions, pass them to log(), then inline, figure out there's no really use of either lambda, and let them vanish.
> 
> Indeed that's quite what happens when compiling with -O -inline -release. The generated _Dmain is the same as a baseline with an empty main(). Once this is in place, a simple version() selector dispatches to either the empty log() above or one that actually logs stuff. Useful to know.

What's wrong with:

    version (Whatever) void log(args) { ... }
    else void log(args) {}
July 12, 2010
On 07/12/2010 01:10 PM, Walter Bright wrote:
> Andrei Alexandrescu wrote:
>> Just saw this on reddit:
>>
>> http://www.reddit.com/r/programming/comments/cocww/little_known_c_feature_conditional_attributes/
>>
>>
>> and was wondering whether this can be done in D by relying on lazy
>> parameters and the inliner. Here's the test bed:
>>
>> void log(A...)(lazy string format, lazy A objects)
>> {
>> }
>>
>> void main(string args[])
>> {
>> log(args[0] ~ "yah", args ~ args, 42 + args.length);
>> }
>>
>> The dependency on args is intended to prevent the optimizer from
>> hoisting evaluation to compilation time.
>>
>> In an ideal world, the compiler should generate lambdas for the three
>> expressions, pass them to log(), then inline, figure out there's no
>> really use of either lambda, and let them vanish.
>>
>> Indeed that's quite what happens when compiling with -O -inline
>> -release. The generated _Dmain is the same as a baseline with an empty
>> main(). Once this is in place, a simple version() selector dispatches
>> to either the empty log() above or one that actually logs stuff.
>> Useful to know.
>
> What's wrong with:
>
> version (Whatever) void log(args) { ... }
> else void log(args) {}

The latter always evaluates args.

Andrei