Unconditional compilation?

Derek Parnell derek at psych.ward
Sun Mar 9 05:08:47 PDT 2008


On Sun, 09 Mar 2008 11:22:29 +0000, John C wrote:

> Code in version blocks must be syntactically correct even if the 
> condition is not met and the code isn't compiled - ie, the compiler 
> still parses the code. But this makes writing libraries that compile 
> with both D1 and D2 difficult - I want to add features for D2 users 
> without having to distribute a separate version of the library.
> 
> version(D_Version2) {
>    struct SomeStruct {
> 
>      // D2 copy ctor
>      this(this) {}
> 
>    }
> }
> 
> D1 chokes on that, which is expected behaviour according to the spec. 
> But how do I get the same behaviour as #if and #else - ie, tell the 
> compiler to completely ignore anything if some condition is not met?

You need to use string mixins.

For example ...

 version(D_Version2) {
    mixin(
    `struct SomeStruct {`
      // D2 copy ctor
    `  this(this) {}`
    `}`
    );
 }

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list