On Friday, 14 March 2014 at 10:30:47 UTC, Rikki Cattermole wrote:
Another option is using pure functions and static if's.

const bool ProgrammingSections = getProgrammingSections;
const bool PasteBinFullSection = getPasteBinFullSection;

pure bool getProgrammingSections() {
        if (CompleteSite) {
                return true;
        } else {
                return false;
        }
}

pure bool getPasteBinFullSection() {
        if (CompleteSite || ProgrammingSections) {
                return true;
        } else {
                return false;
        }
}

static if (ProgrammingSections) {
    // yay programming
}


static if (PasteBinFullSection) {
    // Ooo pastebin!
}


Also works rather well through different modules.

Or for more advanced usage that could be quite useful in phobos:

pure bool isVersionDefinedAnd(ARGS...)() {
        foreach(name; ARGS) {
                mixin("version(" ~ name ~ ") {} else return false;");
        }
        return true;    
}

enum isLinux1And = isVersionDefinedAnd!("linux");
enum isLinux2And = isVersionDefinedAnd!("linux", "Posix");

pragma(msg, isLinux1And);
pragma(msg, isLinux2And);


pure bool isVersionDefinedOr(ARGS...)() {
        foreach(name; ARGS) {
                mixin("version(" ~ name ~ ") return true;");
        }
        return false;   
}

enum isMainPlatform = isVersionDefinedOr!("linux", "Windows", "Posix", "Android", "OSX");

pragma(msg, isMainPlatform);

Reply via email to