The new std.process is ready for review

Lars T. Kyllingstad public at kyllingen.net
Thu Mar 14 13:20:24 PDT 2013


On Wednesday, 13 March 2013 at 20:44:00 UTC, Lars T. Kyllingstad
wrote:
> On Wednesday, 13 March 2013 at 20:26:44 UTC, Steven 
> Schveighoffer wrote:
>> I hate to have feature creep at this point, but one kind of 
>> annoying thing
>> is, if you want to *add* to the current environment, it is a 
>> multi-step
>> process:
>>
>> auto curenv = environment.toAA;
>> curenv["x"] = "y";
>> spawnProcess("helloworld", curenv);
>>
>> 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

The more I think about this, the more it seems like a good idea:

1. A string[string] parameter for the environment, which defaults
to null, and for which there is no difference between null and
empty -- they are both empty.

2. A Config flag that determines whether the given AA should be
merged with the parent's environment or not, with the former
being the default.

3. Variables in the AA always override variables from the
parent's environment.

4. The two spawnProcess() overloads that do *not* take an
environment parameter will be removed.

5. Instead, we add two overloads without the redirection
parameters, since the Config parameter will probably be used more
often now:

    spawnProcess(string prog, string[string] env, Config conf);
    spawnProcess(string[] args, string[string] env, Config conf);

Looks good?

Lars


More information about the Digitalmars-d mailing list