www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope with if

reply berni <berni example.com> writes:
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.
Feb 17 2017
next sibling parent Profile Anaysis <PA gotacha.com> writes:
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"
Feb 17 2017
prev sibling next sibling parent berni <berni example.com> writes:
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.
Feb 18 2017
prev sibling parent Moritz Maxeiner <moritz ucworks.org> writes:
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
Feb 18 2017