digitalmars.D - C#'s conditional attributes
- Andrei Alexandrescu (22/22) Jul 12 2010 Just saw this on reddit:
- Walter Bright (4/33) Jul 12 2010 What's wrong with:
- Andrei Alexandrescu (3/36) Jul 12 2010 The latter always evaluates args.
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
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
On 07/12/2010 01:10 PM, Walter Bright wrote:Andrei Alexandrescu wrote:The latter always evaluates args. AndreiJust 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) {}
Jul 12 2010