Thread overview
Importing version identifiers from another file?
Apr 11, 2022
KytoDragon
Apr 11, 2022
wjoe
Apr 12, 2022
Ali Çehreli
Apr 12, 2022
H. S. Teoh
Apr 17, 2022
cc
April 11, 2022

I am currently maintaining a port of a c++ library that uses conditional compilation to integrate into the users program. e.g.:

config.h (edited by the user of the library)

#define USE_MY_ASSERT
void MY_ASSERT(bool expr) {...}

library.c

include "config.h"
#ifndef USE_MY_ASSERT
    void MY_ASSERT(bool expr) {...} // default implementation
#end

Given that static if at top-level has been broken for some time (https://issues.dlang.org/show_bug.cgi?id=17883, https://issues.dlang.org/show_bug.cgi?id=20905, https://issues.dlang.org/show_bug.cgi?id=21171), I tried to use version identifiers for this in D:

config.d

version = USE_MY_ASSERT;
void MY_ASSERT(bool expr) {...}

library.d

import config;
version (USE_MY_ASSERT) {} else {
    void MY_ASSERT(bool expr) {...}
}

Sadly this results in an identifier conflict, as the version set in config.d does not seem to affect library.d. Is there any way to import version specifiers from a separate file? I don't want to pollute the users build files (dub.json etc.) with dozens of versions they need to pass to the compiler.

April 11, 2022
On Monday, 11 April 2022 at 08:57:12 UTC, KytoDragon wrote:
> [...]
> Sadly this results in an identifier conflict, as the version set in config.d does not seem to affect library.d. Is there any way to import version specifiers from a separate file? I don't want to pollute the users build files (dub.json etc.) with dozens of versions they need to pass to the compiler.

No, version identifiers don't escape module scope.
See 24.1.4 https://dlang.org/spec/version.html for reference.
April 12, 2022
On 4/11/22 01:57, KytoDragon wrote:

> Is there any way to import
> version specifiers from a separate file?

Have you tried mixing in the versions? Have one file with the versions in it:

  // file: 'versions'
  version = x;

Then mix it in:

  mixin (import ("versions"));

  version (x) { /* ... */ }

Seems to work for me.

Ali

April 12, 2022
On Tue, Apr 12, 2022 at 10:07:15AM -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> On 4/11/22 01:57, KytoDragon wrote:
> 
> > Is there any way to import version specifiers from a separate file?
> 
> Have you tried mixing in the versions? Have one file with the versions in it:
> 
>   // file: 'versions'
>   version = x;
> 
> Then mix it in:
> 
>   mixin (import ("versions"));
> 
>   version (x) { /* ... */ }
> 
> Seems to work for me.
[...]

If you need globally-defined versions, I'd just use the command-line option `-version=MyVersionA -version=MyVersionB ...`.

Or, if you absolutely have to define versions in a source file, why not just use enums + static if instead:

	// versions.d
	module versions;
	enum MyVersionA = true;
	enum MyVersionB = true;

	// program.d
	import versions;
	static if (MyVersionA) {
		...
	} else {
		...
	}

	// ... and so on


T

-- 
An elephant: A mouse built to government specifications. -- Robert Heinlein
April 17, 2022
Another option for this was suggested here:

https://forum.dlang.org/post/qbvgboihhwcuqglygzec@forum.dlang.org

On Wednesday, 12 February 2020 at 09:28:15 UTC, Simen Kjærås wrote:
> So, you could have a file called 'versions' containing this:
>
> # Setting 'Compress' version
> -version=Compress
> # Optionally set other versions
> #-version=Foo
> #-version=Bar
>
> and feed it to dmd like so:
>
>     dmd -w -wi -g @versions -main foo.d
>