Re: anonymous functions and scope(exit)

2021-07-04 Thread frame via Digitalmars-d-learn
On Sunday, 4 July 2021 at 10:07:08 UTC, jfondren wrote: Not cleaning up after an Error is thrown is allowed by the D spec. This enhancement allows much better code to be generated for `nothrow` code when `scope` is used. It will also not unwind declarations with destructors in `nothrow` code

Re: anonymous functions and scope(exit)

2021-07-04 Thread Luis via Digitalmars-d-learn
On Sunday, 4 July 2021 at 10:07:08 UTC, jfondren wrote: On Sunday, 4 July 2021 at 08:24:36 UTC, Luis wrote: Dennis's explanation makes the most sense: writeln can throw an Exception, so its presence prevents nothrow inference, which otherwise permits the (not intended to be catchable)

Re: anonymous functions and scope(exit)

2021-07-04 Thread jfondren via Digitalmars-d-learn
On Sunday, 4 July 2021 at 10:07:08 UTC, jfondren wrote: By that, what you're running into is an unpleasant interaction between 1. scope(exit)s that you're writing 2. Errors being thrown rather than Exceptions 3. anonymous functions getting inferred as nothrow And a resolution could be to

Re: anonymous functions and scope(exit)

2021-07-04 Thread jfondren via Digitalmars-d-learn
On Sunday, 4 July 2021 at 08:24:36 UTC, Luis wrote: On Saturday, 3 July 2021 at 22:52:39 UTC, frame wrote: It works if you replace printf() with writeln() or use writeln() after. There must be some buffer issue. Not works as you expected. Yes, replacing by writeln (better said, putting a

Re: anonymous functions and scope(exit)

2021-07-04 Thread Luis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 22:52:39 UTC, frame wrote: On Saturday, 3 July 2021 at 22:04:04 UTC, Luis wrote: scope(exit) it's syntactic sugar for a classic `try {} finally {}` . The documentation says that must be executed. It works if you replace printf() with writeln() or use writeln()

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 22:04:04 UTC, Luis wrote: scope(exit) it's syntactic sugar for a classic `try {} finally {}` . The documentation says that must be executed. It works if you replace printf() with writeln() or use writeln() after. There must be some buffer issue.

Re: anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 20:46:00 UTC, Steven Schveighoffer wrote: On 7/3/21 4:08 PM, frame wrote: On Saturday, 3 July 2021 at 17:39:18 UTC, Steven Schveighoffer wrote: But in practice, the compiler does not have to clean up anything when an `Error` is thrown. Whether it does or not is

Re: anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:47:47 UTC, Dennis wrote: On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: scope(exit) inside of a anonymous functions, it's never called. I think the compiler infers the function `nothrow` since you don't throw any `Exception`, only an `Error`. Errors

Re: anonymous functions and scope(exit)

2021-07-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/3/21 4:08 PM, frame wrote: On Saturday, 3 July 2021 at 17:39:18 UTC, Steven Schveighoffer wrote: But in practice, the compiler does not have to clean up anything when an `Error` is thrown. Whether it does or not is defined by the implementation. This should be really mentionend in the

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:39:18 UTC, Steven Schveighoffer wrote: But in practice, the compiler does not have to clean up anything when an `Error` is thrown. Whether it does or not is defined by the implementation. This should be really mentionend in the docs? "Guard", yeah...

Re: anonymous functions and scope(exit)

2021-07-03 Thread Dennis via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: scope(exit) inside of a anonymous functions, it's never called. I think the compiler infers the function `nothrow` since you don't throw any `Exception`, only an `Error`. Errors represent unrecoverable bugs, after which the program is in

Re: anonymous functions and scope(exit)

2021-07-03 Thread jfondren via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: This is intentional ? ... scope(exit) inside of a anonymous functions, it's never called. ``` $ rdmd --eval 'iota(2).map!((int x) { scope(exit) writeln("got: ", x); return x+1; }).array.writeln' got: 0 got: 1 [1, 2] ``` Conclusion:

Re: anonymous functions and scope(exit)

2021-07-03 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/3/21 1:20 PM, Luis wrote: This is intentional ? ```     should(function void() {     auto emptyStack = SimpleStack!int();     scope(exit) emptyStack.free; // <= This is never called     emptyStack.reserve(16);     emptyStack.top;    

Re: anonymous functions and scope(exit)

2021-07-03 Thread frame via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:20:47 UTC, Luis wrote: This is intentional ? ``` should(function void() { auto emptyStack = SimpleStack!int(); scope(exit) emptyStack.free; // <= This is never called emptyStack.reserve(16);

anonymous functions and scope(exit)

2021-07-03 Thread Luis via Digitalmars-d-learn
This is intentional ? ``` should(function void() { auto emptyStack = SimpleStack!int(); scope(exit) emptyStack.free; // <= This is never called emptyStack.reserve(16); emptyStack.top; }).Throw!RangeError; ``` scope(exit) inside