View mode: basic / threaded / horizontal-split · Log in · Help
November 23, 2012
externally imposed conditional compilation
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.
November 23, 2012
Re: externally imposed conditional compilation
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.
November 23, 2012
Re: externally imposed conditional compilation
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.
November 24, 2012
Re: externally imposed conditional compilation
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
November 24, 2012
Re: externally imposed conditional compilation
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.
November 24, 2012
Re: externally imposed conditional compilation
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).
November 24, 2012
Re: externally imposed conditional compilation
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.
November 25, 2012
Re: externally imposed conditional compilation
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
November 27, 2012
Re: externally imposed conditional compilation
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.
November 27, 2012
Re: externally imposed conditional compilation
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.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home