How do you declare manifest constants?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Nov 4 17:36:24 UTC 2021


On Thu, Nov 04, 2021 at 05:24:44PM +0000, Andrey Zherikov via Digitalmars-d-learn wrote:
> On Thursday, 4 November 2021 at 17:09:31 UTC, Steven Schveighoffer wrote:
> > D doesn't have any equivalent for this.
> 
> Is it possible to add this feature having `-C VERSION="1.2.3"` (`-D`
> is already used) to be equal to `enum VERSION="1.2.3"` in global
> namespace?
> 
> > The closest you can get is to turn on `version` identifiers.
> > 
> > There is also a quirky `-version=123` which is IMO, a completely
> > useless feature.
> 
> I agree - this is useless. `-version myversion=123` would be much more
> useful.

Here's a hack that uses dmd's stdin feature to inject D code into a
compile command:

	// main.d
	import __stdin : myversion;
	void main() {
		import std;
		writeln(myversion);
	}

Compile command:
	echo 'enum myversion = "1.2.3";' | dmd - -run main.d

Output:
	1.2.3

You can change the version number just by changing the echo command. And
of course, it doesn't have to be a string, it can be any valid D type,
and obviously you can declare more than one variable that can then be
read by importing from __stdin.


T

-- 
Don't throw out the baby with the bathwater. Use your hands...


More information about the Digitalmars-d-learn mailing list