C#'s conditional attributes

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jul 12 11:13:11 PDT 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


More information about the Digitalmars-d mailing list