D: How to check if a function is chained? a().b().c();

Julian Fondren julian.fondren at gmail.com
Sat Nov 18 13:57:43 UTC 2023


On Saturday, 18 November 2023 at 07:47:19 UTC, BoQsc wrote:
> `program("someProgramName").pipe("someOtherProgramName");`
> Executes and pipes output to another program.
>
> `program();` - Only executes the program.

Serious answer: have a function handle this, instead of the 
semicolon.

`program("p1").pipe("p2").run;` - does that
`program("p1").run;` - does the other

Supposedly this is the "builder pattern" but the wikipedia entry 
seems to be deliberately bad.

Unserious answer, especially unsuitable for your concrete example 
where you probably want subprocesses to run reliably and in 
order: do something with object lifetime functions.

```d
import std.stdio : writeln;

class Program {
     string program;
     bool used;
     this(string p) { program = p; }
     ~this() { if (!used) writeln("You forgot to chain program: ", 
program); }
}

Program a(string p) {
         return new Program(p);
}

void b(Program p) {
         p.used = true;
         writeln("using program: ", p.program);
}

void main() {
         a("2");
         a("1").b();
}
```


More information about the Digitalmars-d-learn mailing list