improving scope(finally/success)

Marc Schütz via Digitalmars-d digitalmars-d at puremagic.com
Thu Dec 3 06:39:20 PST 2015


On Thursday, 3 December 2015 at 13:07:53 UTC, Idan Arye wrote:
> On Thursday, 3 December 2015 at 11:41:29 UTC, Tomer Filiba 
> wrote:
>> int f() {
>>     scope(exit) writeln("bye");
>>     return 5;
>> }
>>
>> is rewritten as something like
>>
>> int f() {
>>     try {
>>         auto tmp = 5;
>>     }
>>     finally {
>>         writeln("bye");
>>     }
>>     return tmp;
>> }
>>
>> so `tmp` is already there for the finally clause (modulo 
>> scoping issues)
>
> int foo(bool cond) { // scope 1
>    { // scope 2
>        scope(success, retval) {
>            writeln("the retval is", retval)
>        }
>        if (cond) { // scope 3
>            return 1;
>        }
>    }
>    return 2;
> }
>
>
> `foo(true)` should obviously print "the retval is 1", but what 
> should `foo(false)` print? It's `scope(success)` block should 
> run when it exists scope 2, but the return value is only 
> determined at the end of scope 1.

Tomer's suggested lowering can't work, because tmp's scope 
doesn't extend outside of the try. It would need to be hoisted up 
to the beginning of the function.

This would then imply that `foo(false)` would print "0" 
(int.init). However, I don't think this kind of lowering is a 
good idea, because it would create a temporary lvalue, which 
implies that it needs to go through the full 
construct/assign/destroy cycle instead of a simple move, which is 
a potentially observable difference to the current behaviour.


More information about the Digitalmars-d mailing list