Thread overview
scope with if
Feb 17, 2017
berni
Feb 17, 2017
Profile Anaysis
Feb 18, 2017
berni
Feb 18, 2017
Moritz Maxeiner
February 17, 2017
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. 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.

I found a workaround using "finally", but anyway I'm curious.
February 17, 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. 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.
>
> I found a workaround using "finally", but anyway I'm curious.

could be useful to have something like

scope(final)

that would be the "final scope"

February 18, 2017
Just a note - I found something, that works:

>import std.stdio;
>
>void main(string[] args)
>{
>    immutable cond = args[1]=="a";
>    if (cond) write("A");
>    scope (exit) if (cond) write("B");
>
>    write("C");
>}

I'm using the immutable variable to avoid, that the condition changes later.
February 18, 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