digitalmars.D.learn - D alternative for C/C++ -Dfoo=42
- Cherry (6/6) Feb 25 2014 Hello
- Vladimir Panteleev (13/17) Feb 25 2014 No, D does not allow passing values via the compiler command
- Dejan Lekic (18/24) Feb 25 2014 D offers version blocks: http://dlang.org/version.html#version
- Dejan Lekic (4/30) Feb 25 2014 And yes, if someone builds multiple modules, he/she would have to
- bearophile (5/8) Feb 25 2014 If you are interested in such things I suggest you to take a look
- Dejan Lekic (3/11) Feb 25 2014 No, I am not interested, but thanks. I am interested how D does
- Tobias Pankrath (3/16) Feb 25 2014 I don't like it. I would prefer a CTFE-able counter part to
- Dejan Lekic (2/5) Feb 25 2014 If you want a config file, you do not need -D args. You simply
- Tobias Pankrath (10/16) Feb 25 2014 Yep, that would be a cleaner solution since it would avoid some
- Kagamin (3/9) Feb 25 2014 or import myconfig; and specify -I path to myconfig.d instead of
- Cherry (11/14) Feb 25 2014 That would be good.
- Cherry (8/15) Feb 25 2014 For now maybe I could keep an empty/default config.d somewhere in
- Cherry (9/14) Feb 25 2014 I am creating a DSL (Domain Specific Language) library. I am
- Tobias Pankrath (11/16) Feb 25 2014 --
- Francesco Cattoglio (4/22) Feb 25 2014 I thought that too... And then, when I tried, SURPRISE, it
- Francesco Cattoglio (8/31) Feb 25 2014 Ok, MY BAD! It works, but I gave him a folder instead of a file.
Hello I want to define an enum int, but I want to make it possible to set its value when I run dmd from the command line. Is it possible in D? Regards Cherry
Feb 25 2014
On Tuesday, 25 February 2014 at 12:45:06 UTC, Cherry wrote:Hello I want to define an enum int, but I want to make it possible to set its value when I run dmd from the command line. Is it possible in D?No, D does not allow passing values via the compiler command line. You can use the -debug=xxx or -version=xxx to enable debug(xxx) or version(xxx) blocks. For arbitrary values, you will need intermediary files. You can use the import(filename) construct to read a file from disk during compilation, like so: echo 42 > foo.txt cat > test.d import std.conv, std.string; enum foo = to!int(import("foo.txt").strip()); ^D dmd -J. test.d
Feb 25 2014
On Tuesday, 25 February 2014 at 12:45:06 UTC, Cherry wrote:Hello I want to define an enum int, but I want to make it possible to set its value when I run dmd from the command line. Is it possible in D? Regards CherryD offers version blocks: http://dlang.org/version.html#version It would be great if D compiler had a way to substitute enums with given values. (This is a nice candidate for a DIP) Imagine you have a code like this: module myproggy; import std.stdio; enum foo = 5; enum bar = "walter"; int main() { writeln(bar); return foo; } ... and you call compiler like: dmd myproggy.d -Dfoo=42 -Dbar=hello smart, new D compiler would replace enums (iff they exist in the source code) with the given arguments, so when you run myproggy, you get "walter" as output, and the exit code is 42.
Feb 25 2014
On Tuesday, 25 February 2014 at 12:57:51 UTC, Dejan Lekic wrote:On Tuesday, 25 February 2014 at 12:45:06 UTC, Cherry wrote:And yes, if someone builds multiple modules, he/she would have to use fully-qualified name. Example: dmd foo.d bar.d -Dfoo.var1=5 -Dbar.var4=walterHello I want to define an enum int, but I want to make it possible to set its value when I run dmd from the command line. Is it possible in D? Regards CherryD offers version blocks: http://dlang.org/version.html#version It would be great if D compiler had a way to substitute enums with given values. (This is a nice candidate for a DIP) Imagine you have a code like this: module myproggy; import std.stdio; enum foo = 5; enum bar = "walter"; int main() { writeln(bar); return foo; } ... and you call compiler like: dmd myproggy.d -Dfoo=42 -Dbar=hello smart, new D compiler would replace enums (iff they exist in the source code) with the given arguments, so when you run myproggy, you get "walter" as output, and the exit code is 42.
Feb 25 2014
Dejan Lekic:And yes, if someone builds multiple modules, he/she would have to use fully-qualified name. Example: dmd foo.d bar.d -Dfoo.var1=5 -Dbar.var4=walterIf you are interested in such things I suggest you to take a look at how the Fortress language does them. Bye, bearophile
Feb 25 2014
On Tuesday, 25 February 2014 at 13:21:38 UTC, bearophile wrote:Dejan Lekic:No, I am not interested, but thanks. I am interested how D does them, or better say how it may do them...And yes, if someone builds multiple modules, he/she would have to use fully-qualified name. Example: dmd foo.d bar.d -Dfoo.var1=5 -Dbar.var4=walterIf you are interested in such things I suggest you to take a look at how the Fortress language does them. Bye, bearophile
Feb 25 2014
module myproggy; import std.stdio; enum foo = 5; enum bar = "walter"; int main() { writeln(bar); return foo; } ... and you call compiler like: dmd myproggy.d -Dfoo=42 -Dbar=hello smart, new D compiler would replace enums (iff they exist in the source code) with the given arguments, so when you run myproggy, you get "walter" as output, and the exit code is 42.I don't like it. I would prefer a CTFE-able counter part to boost::program_options that generates thouse enums from a config file.
Feb 25 2014
I don't like it. I would prefer a CTFE-able counter part to boost::program_options that generates thouse enums from a config file.If you want a config file, you do not need -D args. You simply import() it. Right?
Feb 25 2014
On Tuesday, 25 February 2014 at 13:26:20 UTC, Dejan Lekic wrote:Yep, that would be a cleaner solution since it would avoid some pitfalls with the current flags proposal: Should those enums be fully qualified? What if I put all the source files into a single command line (which leads to faster compilation with dmd)? May that override enums by accident? Should every enum be overwriteable or only those that have a specific UDA etc. etc. ? And in the end you are using a build tool anyway and you'll put your config flags into a makefile or something that then becomes an ugly config file.I don't like it. I would prefer a CTFE-able counter part to boost::program_options that generates thouse enums from a config file.If you want a config file, you do not need -D args. You simply import() it. Right?
Feb 25 2014
On Tuesday, 25 February 2014 at 13:26:20 UTC, Dejan Lekic wrote:or import myconfig; and specify -I path to myconfig.d instead of -J path.I don't like it. I would prefer a CTFE-able counter part to boost::program_options that generates thouse enums from a config file.If you want a config file, you do not need -D args. You simply import() it. Right?
Feb 25 2014
I don't like it. I would prefer a CTFE-able counter part to boost::program_options that generates thouse enums from a config file.That would be good. I would have taken that approach for my current needs, but I do not see any way to have a fallback if the config file does not exist. I mean mixin import would just fail to compile if the configuration file is not present. I need an option to live with default values if the configuration file is not there. Something like "static if(fileExists!"config.d") mixin(import("config.d")) else ...". Thoughts? Regards - Cherry
Feb 25 2014
I would have taken that approach for my current needs, but I do not see any way to have a fallback if the config file does not exist. I mean mixin import would just fail to compile if the configuration file is not present. I need an option to live with default values if the configuration file is not there. Something like "static if(fileExists!"config.d") mixin(import("config.d")) else ...".For now maybe I could keep an empty/default config.d somewhere in the library source path. And I could use that with multiple -J options. This way dmd configuration from the default path unless it finds the config file in the user path. That should be good enough for me. But I believe this could be made more friendly. Regards - Cherry
Feb 25 2014
Just a few clarifications ....For now maybe I could keep an empty/default config.d somewhere in the library source path.I am creating a DSL (Domain Specific Language) library. I am talking about the library src path of my DSL library.And I could use that with multiple -J options. This way dmd configuration from the default path unless it finds the config file in the user path.I figured that DMD will pick import file from the left-most -J path it finds the file in. So all I need is a wrapper over rdmd/dmd that adds a -J. and -J<default path> (in that order) to the user compile command. Regards - Cherry
Feb 25 2014
Something like "static if(fileExists!"config.d") mixin(import("config.d")) else ...". Thoughts? Regards - Cherry-- static if(__traits(compiles, import("config.d"))) { // import/parse/mixin } else { // default here } -- May be wrapped into a template for more convenience.
Feb 25 2014
On Tuesday, 25 February 2014 at 14:26:13 UTC, Tobias Pankrath wrote:I thought that too... And then, when I tried, SURPRISE, it doesn't work :SSomething like "static if(fileExists!"config.d") mixin(import("config.d")) else ...". Thoughts? Regards - Cherry-- static if(__traits(compiles, import("config.d"))) { // import/parse/mixin } else { // default here } -- May be wrapped into a template for more convenience.
Feb 25 2014
On Tuesday, 25 February 2014 at 14:30:23 UTC, Francesco Cattoglio wrote:On Tuesday, 25 February 2014 at 14:26:13 UTC, Tobias Pankrath wrote:Ok, MY BAD! It works, but I gave him a folder instead of a file. When you give it a folder instead of a file, it still works but also outputs an error: read error, errno = 21 However the "else" branch is still taken. Well, this went better than expected!I thought that too... And then, when I tried, SURPRISE, it doesn't work :SSomething like "static if(fileExists!"config.d") mixin(import("config.d")) else ...". Thoughts? Regards - Cherry-- static if(__traits(compiles, import("config.d"))) { // import/parse/mixin } else { // default here } -- May be wrapped into a template for more convenience.
Feb 25 2014