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 de

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 repre

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 d

Re: how much "real-life" code can be marked @safe ?

2021-07-03 Thread tsbockman via Digitalmars-d-learn
On Saturday, 3 July 2021 at 16:06:33 UTC, Alexandru Ermicioi wrote: 3. An edge case. Ex: You need to mutate some data and then assume it is immutable in a constructor. Can you give a valid example where that is necessary? The main examples that I can think of either can be `@safe` with the rig

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: do we already have sum-modulo-10 algo (aka Luhn's algo) on phobos ?

2021-07-03 Thread jfondren via Digitalmars-d-learn
On Saturday, 3 July 2021 at 17:44:48 UTC, someone wrote: https://en.wikipedia.org/wiki/Luhn_algorithm#Pseudocode_implementation That specific function, in Phobos? no. sum modulo 10? That's just some_var%10 in D. The Wikipedia link ends with a link to RosettaCode: https://rosettacode.org/wiki

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 a

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: it's

do we already have sum-modulo-10 algo (aka Luhn's algo) on phobos ?

2021-07-03 Thread someone via Digitalmars-d-learn
https://en.wikipedia.org/wiki/Luhn_algorithm#Pseudocode_implementation

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;     }).Throw!R

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); emptyStack.top;

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 o

Re: how much "real-life" code can be marked @safe ?

2021-07-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Friday, 2 July 2021 at 22:08:31 UTC, tsbockman wrote: (Responding out of order:) On Friday, 2 July 2021 at 00:26:52 UTC, someone wrote: But when you start attempting to declare @safe chunks of code that actually DO things ... well, it seems end-of-the-story. If you find yourself unable to

Re: How to select the regex that matches the first token of a string?

2021-07-03 Thread vnr via Digitalmars-d-learn
On Saturday, 3 July 2021 at 09:28:32 UTC, user1234 wrote: On Saturday, 3 July 2021 at 09:05:28 UTC, vnr wrote: Hello, I am trying to make a small generic lexer that bases its token analysis on regular expressions. The principle I have in mind is to define a token type table with its correspon

Re: How to select the regex that matches the first token of a string?

2021-07-03 Thread user1234 via Digitalmars-d-learn
On Saturday, 3 July 2021 at 09:05:28 UTC, vnr wrote: Hello, I am trying to make a small generic lexer that bases its token analysis on regular expressions. The principle I have in mind is to define a token type table with its corresponding regular expression, here is the code I currently have

How to select the regex that matches the first token of a string?

2021-07-03 Thread vnr via Digitalmars-d-learn
Hello, I am trying to make a small generic lexer that bases its token analysis on regular expressions. The principle I have in mind is to define a token type table with its corresponding regular expression, here is the code I currently have: ```d import std.regex; /// ditto struct Token {