www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - externally imposed conditional compilation

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Hi, D community.

I have C code (*cough* WinAPI *cough*) like:

#if FOOBAR == 1
#define SOMETHING
#elif FOOBAR > 2
#define SOMETHING
#elif FOOBAR < 5
#define SOMETHING
#endif

Which I want to translate to D.

Here are the problems I have:
1. I can't use versions because:
    1.1. The identifier version won't work because the set of values for
FOOBAR is unknown, thus cannot be enumerated as different version
identifiers.
    1.2. The number version won't work because there are tons of other
things like FOOBAR and they can't all use the version number.
2. I can't use static if because:
    2.1. I can't define FOOBAR from outside of the package this code will
be in.
    2.2. I can't include the definition of FOOBAR into the package (like
config.d) and expect it to be changed as necessary, because the package
encapsulation will be broken.

What's the best/standard way translating this to D?

-- 
Bye,
Gor Gyolchanyan.
Nov 23 2012
next sibling parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Friday, 23 November 2012 at 11:38:31 UTC, Gor Gyolchanyan 
wrote:
 What's the best/standard way translating this to D?
One way is to use import expressions: import std.stdio; import std.conv; enum value = import("value").to!int(); void main() { static if (value < 100) writeln("<100"); else writeln(">100"); } Compile using: dmd -Jpath/to/value source.d "value" should be a file containing a single integer with no new line. It will be read in at compile time and converted to a manifest integer constant, which you can use in static if statements.
Nov 23 2012
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Thanks! I completely forgot about import expressions! I can just as well
supply a config.d with all necessary definitions!

I thing another way is to have the entire module a template and instantiate
that template with necessary parameters. Although that means one has to
have an additional namespace.


On Sat, Nov 24, 2012 at 2:01 AM, Peter Alexander <
peter.alexander.au gmail.com> wrote:

 On Friday, 23 November 2012 at 11:38:31 UTC, Gor Gyolchanyan wrote:

 What's the best/standard way translating this to D?
One way is to use import expressions: import std.stdio; import std.conv; enum value = import("value").to!int(); void main() { static if (value < 100) writeln("<100"); else writeln(">100"); } Compile using: dmd -Jpath/to/value source.d "value" should be a file containing a single integer with no new line. It will be read in at compile time and converted to a manifest integer constant, which you can use in static if statements.
-- Bye, Gor Gyolchanyan.
Nov 23 2012
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 23 November 2012 at 22:20:39 UTC, Gor Gyolchanyan 
wrote:
 I thing another way is to have the entire module a template and 
 instantiate
 that template with necessary parameters. Although that means 
 one has to
 have an additional namespace.
Mixin the template into the module scope? http://dpaste.dzfl.pl/b140509f
Nov 23 2012
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
In case of WinAPI itself, which takes a dozen of preprocessor definitions,
would you like a WinAPI binding, where you need to mix in the WinAPI?


On Sat, Nov 24, 2012 at 5:12 AM, Vladimir Panteleev <
vladimir thecybershadow.net> wrote:

 On Friday, 23 November 2012 at 22:20:39 UTC, Gor Gyolchanyan wrote:

 I thing another way is to have the entire module a template and
 instantiate
 that template with necessary parameters. Although that means one has to
 have an additional namespace.
Mixin the template into the module scope? http://dpaste.dzfl.pl/b140509f
-- Bye, Gor Gyolchanyan.
Nov 23 2012
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Saturday, 24 November 2012 at 05:55:49 UTC, Gor Gyolchanyan 
wrote:
 In case of WinAPI itself, which takes a dozen of preprocessor 
 definitions,
 would you like a WinAPI binding, where you need to mix in the 
 WinAPI?
I believe the bindings on dsource use a sensible solution. It is similar to what is used in e.g. Pascal, the preprocessor of which doesn't support define values or comparisons (only IFDEF).
Nov 23 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
which is: take a version flag, make a enum out of it and compare it as you
want. right?


On Sat, Nov 24, 2012 at 10:39 AM, Vladimir Panteleev <
vladimir thecybershadow.net> wrote:

 On Saturday, 24 November 2012 at 05:55:49 UTC, Gor Gyolchanyan wrote:

 In case of WinAPI itself, which takes a dozen of preprocessor definitions,
 would you like a WinAPI binding, where you need to mix in the WinAPI?
I believe the bindings on dsource use a sensible solution. It is similar to what is used in e.g. Pascal, the preprocessor of which doesn't support define values or comparisons (only IFDEF).
-- Bye, Gor Gyolchanyan.
Nov 23 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-11-23 12:38, Gor Gyolchanyan wrote:

 2. I can't use static if because:
      2.1. I can't define FOOBAR from outside of the package this code
 will be in.
      2.2. I can't include the definition of FOOBAR into the package
 (like config.d) and expect it to be changed as necessary, because the
 package encapsulation will be broken.
What about using version identifiers to set constants and the use static-if? Something like this: https://github.com/jacob-carlborg/dvm/blob/d1/dvm/util/Version.d -- /Jacob Carlborg
Nov 25 2012
next sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Nice idea, especially with the template to make things easier. Still it
looks ugly. Version identifiers and numbers (as well as debug identifiers
and numbers) serve a very good purpose (parametrizing modules), but they're
vastly incomplete. There's so much useful stuff that could be done if
module could have access to data of user defined types. I think this is
very close to the idea of UDAs. If UDAs become mutable (which I think would
be the most important feature of UDAs), then the module declaration could
also get mutable UDAs, which would solve all problems. Don't you think? And
version identifiers can be used as described in Version.d, except they'll
change the module UDAs instead of defining a manifest constant.


On Sun, Nov 25, 2012 at 3:56 PM, Jacob Carlborg <doob me.com> wrote:

 On 2012-11-23 12:38, Gor Gyolchanyan wrote:

  2. I can't use static if because:
      2.1. I can't define FOOBAR from outside of the package this code
 will be in.
      2.2. I can't include the definition of FOOBAR into the package
 (like config.d) and expect it to be changed as necessary, because the
 package encapsulation will be broken.
What about using version identifiers to set constants and the use static-if? Something like this: https://github.com/jacob-**carlborg/dvm/blob/d1/dvm/util/**Version.d<https://github.com/jacob-carlborg/dvm/blob/d1/dvm/util/Version.d> -- /Jacob Carlborg
-- Bye, Gor Gyolchanyan.
Nov 26 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-11-27 08:22, Gor Gyolchanyan wrote:
 Nice idea, especially with the template to make things easier. Still it
 looks ugly. Version identifiers and numbers (as well as debug
 identifiers and numbers) serve a very good purpose (parametrizing
 modules), but they're vastly incomplete. There's so much useful stuff
 that could be done if module could have access to data of user defined
 types. I think this is very close to the idea of UDAs. If UDAs become
 mutable (which I think would be the most important feature of UDAs),
 then the module declaration could also get mutable UDAs, which would
 solve all problems. Don't you think? And version identifiers can be used
 as described in Version.d, except they'll change the module UDAs instead
 of defining a manifest constant.
Do you have any example of how you would like this to look like? -- /Jacob Carlborg
Nov 26 2012
prev sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I'd go one step forward to also allow UDAs on packages. This will be very
useful for conditionally compiling entire libraries, instead of manually
setting the same UDAs for every module in the package.


On Tue, Nov 27, 2012 at 11:22 AM, Gor Gyolchanyan <
gor.f.gyolchanyan gmail.com> wrote:

 Nice idea, especially with the template to make things easier. Still it
 looks ugly. Version identifiers and numbers (as well as debug identifiers
 and numbers) serve a very good purpose (parametrizing modules), but they're
 vastly incomplete. There's so much useful stuff that could be done if
 module could have access to data of user defined types. I think this is
 very close to the idea of UDAs. If UDAs become mutable (which I think would
 be the most important feature of UDAs), then the module declaration could
 also get mutable UDAs, which would solve all problems. Don't you think? And
 version identifiers can be used as described in Version.d, except they'll
 change the module UDAs instead of defining a manifest constant.


 On Sun, Nov 25, 2012 at 3:56 PM, Jacob Carlborg <doob me.com> wrote:

 On 2012-11-23 12:38, Gor Gyolchanyan wrote:

  2. I can't use static if because:
      2.1. I can't define FOOBAR from outside of the package this code
 will be in.
      2.2. I can't include the definition of FOOBAR into the package
 (like config.d) and expect it to be changed as necessary, because the
 package encapsulation will be broken.
What about using version identifiers to set constants and the use static-if? Something like this: https://github.com/jacob-**carlborg/dvm/blob/d1/dvm/util/**Version.d<https://github.com/jacob-carlborg/dvm/blob/d1/dvm/util/Version.d> -- /Jacob Carlborg
-- Bye, Gor Gyolchanyan.
-- Bye, Gor Gyolchanyan.
Nov 26 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-11-27 08:25, Gor Gyolchanyan wrote:
 I'd go one step forward to also allow UDAs on packages. This will be
 very useful for conditionally compiling entire libraries, instead of
 manually setting the same UDAs for every module in the package.
How would that work? You cannot declare a package, only modules. -- /Jacob Carlborg
Nov 26 2012
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
There's a tiny, but extremely useful enhancement request, that resides in
bugzilla for a long time now. It proposes the possibility to have modules
named "package", in which case they're imported using their package name
alone:

mylib
    core.d
    aux.d
    package.d

// main.d:
    import mylib.core;
    import mylib.aux;
    import mylib;

In this case adding UDAs to packages essentially means adding UDAs to the
"package" module.
And adding UDAs to any modules would look like this:

[MyUda] module mylib.core;

Since the module-level import statements affect the entire module, the
later imports would be enough to make the MyUda type visible at the top.


On Tue, Nov 27, 2012 at 11:42 AM, Jacob Carlborg <doob me.com> wrote:

 On 2012-11-27 08:25, Gor Gyolchanyan wrote:

 I'd go one step forward to also allow UDAs on packages. This will be
 very useful for conditionally compiling entire libraries, instead of
 manually setting the same UDAs for every module in the package.
How would that work? You cannot declare a package, only modules. -- /Jacob Carlborg
-- Bye, Gor Gyolchanyan.
Nov 26 2012