The new std.process is ready for review
H. S. Teoh
hsteoh at quickfur.ath.cx
Wed Mar 13 14:02:15 PDT 2013
On Wed, Mar 13, 2013 at 09:43:59PM +0100, Lars T. Kyllingstad wrote:
> On Wednesday, 13 March 2013 at 20:26:44 UTC, Steven Schveighoffer
> wrote:
[...]
> >But with something similar to Dennis' idea, we have a possible way to
> >do that without making a copy of the current environment into an AA
> >and adding:
> >
> >struct EnvironmentArg
> >{
> > this(string[string] env, bool useParent=false) { this.env =
> >env;
> >this.useParent = useParent;}
> > this(bool useParent) {this.useParent = useParent;}
> > string[string] env;
> > bool useParent;
> >}
> >
> >spawnProcess("helloworld", EnvironmentArg(["x":"y"], true)); // use parent
> >environment, add x=y
> >spawnProcess("helloworld", EnvironmentArg(["x":"y"])); // replace environment with x=y
> >spawnProcess("helloworld", EnvironmentArg(false)); // use empty environment
> >spawnProcess("helloworld", EnvironmentArg(true)); // use parent environment exactly
> >
> >EnvironmentArg should probably have better name, and I would
> >recommend some global functions that make common things, like:
> >
> >EnvironmentArg emptyEnvironment() { return EnvironmentArg(null, false);}
> >EnvironmentArg parentEnvironment() { return EnvironmentArg(null, true);}
> >
> >Like? Hate?
>
> Hmm.. what if spawnProcess() takes a normal string[string] like it
> does now, but we add a flag to Config that determines whether it is
> merged with the parent's environment or not?
>
> string[string] myEnv = [ "foo" : "bar" ];
> spawnProcess("helloworld", null); // Parent's env
> spawnProcess("helloworld", myEnv); // Parent's env + myEnv
> spawnProcess("helloworld", null, ..., Config.clearEnv); // Empty env
> spawnProcess("helloworld", myEnv, ..., Config.clearEnv); // Only myEnv
[...]
+1. I like this idea. Makes code more self-documenting, which is a good
thing.
Alternatively:
struct useParentEnv { string[string] aa; }
struct newEnv { string[string] aa; }
spawnProcess(E)(string program, E env, ...) {
static if (is(E == useParentEnv))
// inherit values from current environment
else
// don't inherit from current environment
}
spawnProcess("helloworld", useParentEnv(["a": "b", ... ]), ...);
spawnProcess("helloworld", newEnv(["a": "b", ... ]), ...);
Of course, rename useParentEnv and newEnv to something more suitable.
T
--
"You know, maybe we don't *need* enemies." "Yeah, best friends are about all I can take." -- Calvin & Hobbes
More information about the Digitalmars-d
mailing list