Pre-import version statements

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jul 20 06:44:30 UTC 2023


On Wednesday, July 19, 2023 10:56:26 PM MDT Chris Piker via Digitalmars-d-
learn wrote:
> Hi D
>
> In my C code I used to typically put the line:
> ```
> #define _POSIX_C_SOURCE 200112L
> ```
> in the source before importing any standard library headers.  Is
> there something equivalent for phobos?  Say
> `version(phobos2.100)` or similar?
>
> I don't particularly need this functionality, just checking to
> see if there as a formal way to denote what library standard the
> application code is expect to work against.
>
> Thanks,

D has nothing equivalent to that. You compile your code with whichever
version of dmd (or ldc, gdc, etc.) that you want, and it either compiles or
it doesn't. The features of the compiler and the standard library go hand in
hand, and you can't do something like use dmd version X while saying that
you want Phobos version Y. They're both going to be the same version. The
compiler may have additional switches to turn features on and off (e.g. to
enable checks for functionality that's being added as part of a DIP but
isn't the default behavior yet), but the compiler and standard library are
versioned together.

The closest thing to what you're talking about that I can think of in D
would be __VERSION__, which simply gives you access to the version of the
compiler that you're compiling with. So, it's compiler-specific, and all it
gives you is an integer value. As such, outside of very rare cases, it would
likely be a bad idea to actually use it for any form of conditional
compilation.

https://dlang.org/spec/lex.html#specialtokens

Typically, the approach that people use it to just always use the latest
compiler. And outside of deprecations being removed or bad luck with bug
fixes, code usually just continues to work. And if they need to stick to an
older version of the compiler for some reason, they just use that version of
the compiler. But writing code that explicitly depends on a particular
version of the compiler is not something that many projects are likely to be
doing.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list