scope with if

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Feb 18 14:34:47 PST 2017


On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:
> I wonder if it's possible to do something like this:
>
>>import std.stdio;
>>
>>void main(string[] args)
>>{
>>    if (args[1]=="a")
>>    {
>>        write("A");
>>        scope (exit) write("B");
>>    }
>>
>>    write("C");
>>}
>
> I expected the output to be ACB not ABC.

Scope guards are documented here[1][2] and that example behaves 
according to the spec. You can reach what I understood to be your 
objective by implementing the desired functionality on top of a 
scope guard, though:

---
import std.stdio;

void main(string[] args)
{
     void delegate()[] finalizers;
     scope (exit) foreach (onExit; finalizers) onExit();

     if (args.length >= 2 && args[1] == "a")
     {
         writeln("A");
         finalizers ~= { writeln("B"); };
     }

     writeln("C");
}
---

Keep the following in mind, though[2]:
> A scope(exit) or scope(success) statement may not exit with a 
> throw, goto, break, continue, or return; nor may it be entered 
> with a goto.
i.e. those finalizers must not throw.

On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:
> I understand, that the scope ends at the end of the if, but I 
> wonder, if it's possible to have a "conditional scope" or 
> something like this.

As shown in the example above, if you want functionality that is 
not provided by the default scope guard behaviour you'll have to 
implement it on top of them.

[1] https://tour.dlang.org/tour/en/gems/scope-guards
[2] https://dlang.org/spec/statement.html#ScopeGuardStatement



More information about the Digitalmars-d-learn mailing list