Why is D unpopular?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 13 17:59:12 UTC 2022


On Fri, May 13, 2022 at 05:35:35PM +0000, IGotD- via Digitalmars-d wrote:
> On Friday, 13 May 2022 at 17:27:01 UTC, H. S. Teoh wrote:
> > 
> > As for #define'd manifest constants, wouldn't it just be a matter of
> > adding a separate pass over the .h file to extract #define's that
> > look like they can be transformed into enum constants?  Shouldn't be
> > *that* hard to do, in theory anyway.
> > 
> 
> There must be a similar way as with C compilers (the -D command line
> option) to inject defines. The D compiler must add this so that import
> C constants can be defined at the command line otherwise we have that
> extra custom build step again.

Poor man's workaround:

------ main.d ------
import std;
import __stdin;
void main() {
	writeln(MY_VALUE);
}
--------------------

------ command line ------
echo 'enum MY_VALUE = 123;' | dmd - -run main.d
--------------------------

------ output ------
123
--------------------

;-)

This isn't limited to defining constants; you can inject arbitrary
snippets of D code into a compilation this way.  It isn't an *ideal*
solution, of course.  But it's possible.

Though unfortunately, I don't think this will work with ImportC. As in,
the compiler won't know to invoke the preprocessor with the appropriate
-D... flags, so if the .h file depends on some identifier like, e.g.,
_GNU_SOURCE, being predefined, then it won't work.


T

-- 
Only boring people get bored. -- JM


More information about the Digitalmars-d mailing list