| Thread overview | |||||
|---|---|---|---|---|---|
|
October 08, 2014 compile time configurations | ||||
|---|---|---|---|---|
| ||||
I recently thought of the idea of using string imports for
compile time configuration.
Something like this
---
import std.stdio;
import std.conv;
import std.range;
import std.algorithm;
string getVar(string fname,string var)(){
foreach(property;import(fname).splitter('\n')){
auto varname=property[0..property.countUntil("=")];
if(var==varname){
return property[property.countUntil("=")+1..$];
}
}
assert(0, "unable to find property");
}
enum dimisions=getVar!("shapes","dimisions").to!uint;
void main()
{
writeln("There are ",dimisions," dimisions");
}
---
Where you add your config dir(that contains "shapes") file to
dub's stringImportPaths".
How do you guys feel about this? Should we use something like
json for config files?
| ||||
October 10, 2014 Re: compile time configurations | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Freddy | Am Wed, 08 Oct 2014 22:34:36 +0000 schrieb "Freddy" <Hexagonalstar64@gmail.com>: > I recently thought of the idea of using string imports for > compile time configuration. > Something like this > --- > import std.stdio; > import std.conv; > import std.range; > import std.algorithm; > > string getVar(string fname,string var)(){ > foreach(property;import(fname).splitter('\n')){ > auto varname=property[0..property.countUntil("=")]; > if(var==varname){ > return property[property.countUntil("=")+1..$]; > } > } > assert(0, "unable to find property"); > } > > enum dimisions=getVar!("shapes","dimisions").to!uint; > > void main() > { > writeln("There are ",dimisions," dimisions"); > } > --- > Where you add your config dir(that contains "shapes") file to > dub's stringImportPaths". > How do you guys feel about this? Should we use something like > json for config files? I did something similar a while back. It parsed a .ini that contained default values and created a statically typed runtime parser for the actual .ini file from it. It was fun, to see that you can do that with D. -- Marco | |||
October 11, 2014 Re: compile time configurations | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Marco Leise | > I did something similar a while back. It parsed a .ini that
> contained default values and created a statically typed runtime
> parser for the actual .ini file from it. It was fun, to see
> that you can do that with D.
Yes. I use import('filename') to configure my application too. I have config.json, where I store standard application's file paths, options and so on. It's very handy compiler's feature
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply