On Tuesday, 10 May 2016 at 11:12:58 UTC, Tomer Filiba wrote:
Hey guys,

Looking at our code base (weka), I realized it would be really useful if we had logical operators (negation, conjunction, disjunction) in the version's "condition" clause. Here's what I have in mind:

version(!extra_checks) {
    ...
}

version(dlang_ver_2069 || dlang_ver_2070) {
    ...
}

Today we have lots of ugly code like

version(unittest) {} else { ... }

and bad-old copy-paste for the logical-or case I mentioned.

Is there any reason for that versions can't take compound logical conditions? Alternatively, an isVersion(x) predicate that I could use in a static if could do the trick -- although I think ``version(x || y)`` is more readable than ``static if (isVersion(x) || isVersion(y))``

I think it could be a useful feature. Any thoughts?

-tomer

This has been discussed multiple times in the forum, Walter is against it and I agree with him:

http://forum.dlang.org/thread/[email protected]
http://forum.dlang.org/thread/[email protected]

Static if can do this now using enums that you define by using those version conditions, but Walter advises against it.

Another alternative is to put all such OS versioning logic in a build script somewhere and then version on features in your D code, which is probably cleanest, ie have a build script check if you're building for an OS that supports LIBRARY_X, then pass -version=LIBRARY_X to your build and version your D code using version(LIBRARY_X). If you use reggae (https://github.com/atilaneves/reggae), the logic that checks if you're using LIBRARY_X could be written in D too.

The idea is to avoid having a bunch of such repeated conditional logic for versioning spread throughout the codebase, because it will be very error-prone and brittle.

Reply via email to