www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C#'s conditional attributes

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Jul 12 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
Andrei Alexandrescu wrote:
 Just saw this on reddit:
 
 http://www.reddit.com/r/programming/comments/cocww/little_known_c_feature_con
itional_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) {}
Jul 12 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Jul 12 2010