digitalmars.D.learn - scope with if
- berni (5/15) Feb 17 2017 I expected the output to be ACB not ABC. I understand, that the
- Profile Anaysis (4/21) Feb 17 2017 could be useful to have something like
- berni (3/11) Feb 18 2017 I'm using the immutable variable to avoid, that the condition
- Moritz Maxeiner (27/47) Feb 18 2017 Scope guards are documented here[1][2] and that example behaves
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
On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:I wonder if it's possible to do something like this:could be useful to have something like scope(final) that would be the "final scope"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
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
On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:I wonder if it's possible to do something like this: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]: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.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