global vs context variable

luka8088 luka8088 at owave.net
Wed Dec 11 05:01:28 PST 2013


On 11.12.2013. 12:14, Shammah Chancellor wrote:
> On 2013-12-11 08:21:35 +0000, luka8088 said:
> 
>> Examples using such library:
>>
>> void writeOutput () {
>>   writeln("example output");
>> }
>>
>> void main () {
>>
>>   writeOutput();
>>
>>   standardOutputContext(file("example.txt"), {
>>     writeOutput();
>>   });
>>
>> }
> 
> 
> What does this method have over just using:
> 
> with(file("example.txt"))
> {
>     writeln("Foo");
> }
> 

It works with deep nesting without the need to pass the context as a
function argument:

void writeOutput () {
  writeln("example output");
}

void f2 () {
  writeOutput();
}

void f1 () {
  f2();
}

void main () {

  f1();

  standardOutputContext(file("example.txt"), {
    f1();
  });
}



More information about the Digitalmars-d mailing list