Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread kinke via Digitalmars-d-learn
On Monday, 4 May 2020 at 11:50:49 UTC, Steven Schveighoffer wrote: I'm not sure if Ali is referring to this, but the usage of scope to allocate on the stack was at one time disfavored by the maintainers. This is why std.typecons.scoped was added (to hopefully remove that feature). Though, if

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, May 04, 2020 at 09:33:27AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: [...] > Now it's news to me that 'new' does not allocate on the heap when > 'scope' is used. I'm not sure I'm comfortable with it but that's true. > Is this unique? Otherwise, 'new' always allocates on the heap,

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/4/20 12:33 PM, Ali Çehreli wrote: Now it's news to me that 'new' does not allocate on the heap when 'scope' is used. I'm not sure I'm comfortable with it but that's true. Is this unique? Otherwise, 'new' always allocates on the heap, no? This feature was in D1 AFAIK. So it's really old

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 5/4/20 2:47 AM, Olivier Pisano wrote: On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote: On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote: > I think you want to use scope rather than auto which will put the class > on the stack

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/4/20 5:47 AM, Olivier Pisano wrote: On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote: On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote: > I think you want to use scope rather than auto which will put the class > on the stack

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread Olivier Pisano via Digitalmars-d-learn
On Monday, 4 May 2020 at 09:20:06 UTC, Ali Çehreli wrote: On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote: > I think you want to use scope rather than auto which will put the class > on the stack and call its destructor: >

Re: Idomatic way to guarantee to run destructor?

2020-05-04 Thread Ali Çehreli via Digitalmars-d-learn
On 4/30/20 10:04 AM, Ben Jones wrote:> On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote: > I think you want to use scope rather than auto which will put the class > on the stack and call its destructor: > https://dlang.org/spec/attribute.html#scope That is correct about

Re: Idomatic way to guarantee to run destructor?

2020-05-03 Thread Robert M. Münch via Digitalmars-d-learn
On 2020-05-02 20:43:16 +, Steven Schveighoffer said: destroy sets all the values to the .init value. And it nulls the vtable pointer. Ok, that makes sense. Thanks for all the deep internal details. There is always a lot to learn. -- Robert M. Münch http://www.saphirion.com smarter |

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/2/20 3:08 PM, Robert M. Münch wrote: On 2020-05-02 18:18:44 +, Steven Schveighoffer said: On 5/2/20 4:44 AM, Robert M. Münch wrote: How would that help, because the class instance is now unusable anyway. So I have it around like a zombie and others might think: "Hey you look

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn
On 2020-05-02 18:18:44 +, Steven Schveighoffer said: On 5/2/20 4:44 AM, Robert M. Münch wrote: How would that help, because the class instance is now unusable anyway. So I have it around like a zombie and others might think: "Hey you look normal, let's get in contact" and then you are

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Steven Schveighoffer via Digitalmars-d-learn
On 5/2/20 4:44 AM, Robert M. Münch wrote: On 2020-04-30 17:45:24 +, Steven Schveighoffer said: You can use scope instead of auto, and it will then allocate the class on the stack, and destroy it as Ben Jones said. There is danger there, however, as it's very easy to store a class

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn
On 2020-04-30 17:45:24 +, Steven Schveighoffer said: No, auto is declaring that there's about to be a variable here. In actuality, auto does nothing in the first case, it just means local variable. But without the type name, the type is inferred (i.e. your second example). This does not

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn
On 2020-04-30 17:04:43 +, Ben Jones said: I think you want to use scope rather than auto which will put the class on the stack and call its destructor: https://dlang.org/spec/attribute.html#scope Yes, thanks. -- Robert M. Münch http://www.saphirion.com smarter | better | faster

Re: Idomatic way to guarantee to run destructor?

2020-04-30 Thread Steven Schveighoffer via Digitalmars-d-learn
On 4/30/20 12:55 PM, Robert M. Münch wrote: For ressource management I mostly use this pattern, to ensure the destructor is run: void myfunc(){  MyClass X = new MyClass(); scope(exit) X.destroy; } I somewhere read, this would work too: void myfunc(){  auto MyClass X = new MyClass(); }

Re: Idomatic way to guarantee to run destructor?

2020-04-30 Thread Ben Jones via Digitalmars-d-learn
On Thursday, 30 April 2020 at 16:55:36 UTC, Robert M. Münch wrote: For ressource management I mostly use this pattern, to ensure the destructor is run: void myfunc(){ MyClass X = new MyClass(); scope(exit) X.destroy; } I somewhere read, this would work too: void myfunc(){ auto MyClass X =