Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-17 Thread tchaloupka via Digitalmars-d-learn
On Friday, 16 October 2020 at 16:00:07 UTC, Steven Schveighoffer wrote: On 10/16/20 9:12 AM, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior expected?

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 9:05 AM, Steven Schveighoffer wrote: > The destruction of members is outside the destructor's purview. It can't > turn the destruction off, so it should logically be considered part of > an enclosing function. Thank you. Makes sense. Ali

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 11:11 AM, Ali Çehreli wrote: On 10/16/20 6:12 AM, tchaloupka wrote: > struct Foo { >  Bar bar; >  bool err; > >  ~this() { >  // scope(failure) destroy(bar); // < this fixes the Bar > destructor call >  enforce(!err, "Test err"); Well, that check

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/16/20 9:12 AM, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior expected? I would say it's a bug. The compiler is going to call the member

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 October 2020 at 15:19:51 UTC, Paul Backus wrote: On Friday, 16 October 2020 at 13:12:04 UTC, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 October 2020 at 13:12:04 UTC, tchaloupka wrote: So when the exception is thrown within Foo destructor (and it's bad on it's own but can easily happen as destructors aren't nothrow @nogc by default). Is this behavior expected? This is a compiler/language bug. It was fixed in

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 6:12 AM, tchaloupka wrote: > struct Foo { > Bar bar; > bool err; > > ~this() { > // scope(failure) destroy(bar); // < this fixes the Bar > destructor call > enforce(!err, "Test err"); Well, that check means "cannot continue", which means the compiler

Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread tchaloupka via Digitalmars-d-learn
Found a pretty nasty bug in vibe-d: https://github.com/vibe-d/vibe.d/issues/2484 And it's caused by this behavior. ```D import std; struct Foo { Bar bar; bool err; ~this() { // scope(failure) destroy(bar); // < this fixes the Bar destructor call enforce(!err,

Re: Struct destructor

2019-03-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, March 2, 2019 4:32:53 AM MST JN via Digitalmars-d-learn wrote: > Compare this D code: > > import std.stdio; > > struct Foo > { > ~this() > { > writeln("Destroying foo"); > } > } > > void main() > { > Foo[string] foos; > > foos["bar"] = Foo(); >

Re: Struct destructor

2019-03-02 Thread Matheus via Digitalmars-d-learn
/dlang.org/spec/struct.html#struct-destructor "Destructors are called when an object goes out of scope. Their purpose is to free up resources owned by the struct object." Example: import std.stdio; struct S { ~this() { import std.stdio; writeln(&q

Struct destructor

2019-03-02 Thread JN via Digitalmars-d-learn
Compare this D code: import std.stdio; struct Foo { ~this() { writeln("Destroying foo"); } } void main() { Foo[string] foos; foos["bar"] = Foo(); writeln("Preparing to destroy"); foos.remove("bar"); writeln("Ending program"); } and equivalent C++ code:

Re: Disabling struct destructor illegal?

2018-07-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/19/18 4:50 AM, RazvanN wrote: struct A {     int a;     @disable ~this() {} } void main() {     A a = A(2); } Currently, this code yields: Error: destructor `A.~this` cannot be used because it is annotated with @disable I was expecting that disabling the destructor would make it as

Re: Disabling struct destructor illegal?

2018-07-20 Thread Jim Balter via Digitalmars-d-learn
On Thursday, 19 July 2018 at 10:04:34 UTC, RazvanN wrote: On Thursday, 19 July 2018 at 09:50:32 UTC, Jim Balter wrote: On Thursday, 19 July 2018 at 08:50:15 UTC, RazvanN wrote: struct A { int a; @disable ~this() {} } void main() { A a = A(2); } Currently, this code yields:

Re: Disabling struct destructor illegal?

2018-07-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, July 19, 2018 10:04:34 RazvanN via Digitalmars-d-learn wrote: > I just don't understand why you would ever mark the destructor of > a struct with @disable. When is that useful? If it's not, why not > just forbit it? There's nothing special about destructors here. You can @disable any

Re: Disabling struct destructor illegal?

2018-07-19 Thread Simen Kjærås via Digitalmars-d-learn
On Thursday, 19 July 2018 at 10:04:34 UTC, RazvanN wrote: I just don't understand why you would ever mark the destructor of a struct with @disable. When is that useful? If it's not, why not just forbit it? struct S1 { ~this() { /* stuff */ } } struct S2 { S1 s; @disable ~this();

Re: Disabling struct destructor illegal?

2018-07-19 Thread RazvanN via Digitalmars-d-learn
On Thursday, 19 July 2018 at 09:50:32 UTC, Jim Balter wrote: On Thursday, 19 July 2018 at 08:50:15 UTC, RazvanN wrote: struct A { int a; @disable ~this() {} } void main() { A a = A(2); } Currently, this code yields: Error: destructor `A.~this` cannot be used because it is

Re: Disabling struct destructor illegal?

2018-07-19 Thread Jim Balter via Digitalmars-d-learn
On Thursday, 19 July 2018 at 08:50:15 UTC, RazvanN wrote: struct A { int a; @disable ~this() {} } void main() { A a = A(2); } Currently, this code yields: Error: destructor `A.~this` cannot be used because it is annotated with @disable I was expecting that disabling the

Disabling struct destructor illegal?

2018-07-19 Thread RazvanN via Digitalmars-d-learn
struct A { int a; @disable ~this() {} } void main() { A a = A(2); } Currently, this code yields: Error: destructor `A.~this` cannot be used because it is annotated with @disable I was expecting that disabling the destructor would make it as if the struct does not have a

Re: need to emulate scope(failure) with struct destructor

2017-05-28 Thread Dukc via Digitalmars-d-learn
. Another less likely, but possible, way is to have the struct destructor to do the scope(exit) part only if it gets destroyed in some certain state. For example, if the transaction receiver is null.

need to emulate scope(failure) with struct destructor

2017-05-28 Thread piotrklos via Digitalmars-d-learn
I need to perform an action, in multiple separate functions, if scope exits with an exception. The trouble is I don't want to litter my code with scope(failure) everywhere. I already create an instance of a struct at each location, with the sole purpose of doing things at the end of scope. So

Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 17:49:22 UTC, kinke wrote: Basic stuff such as this is appropriately tested. The named return value optimization is enforced by D (incl. unoptimized builds), so behavior doesn't change by this optimization. It's you who changed the behavior by removing the if.

Re: returning struct, destructor

2016-12-21 Thread Ali Çehreli via Digitalmars-d-learn
On 12/21/2016 07:01 AM, Eugene Wissner wrote: > Isn't an optimization that changes the behavior bad? I had a crash in > the code where the destructor did something meaningfull, freed the > memory (the same pointer) twice. Sounds like you ended up with two objects that owned the same resource.

Re: returning struct, destructor

2016-12-21 Thread kinke via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 18:02:54 UTC, bauss wrote: It removes an unnecessary allocation for the returning copy of the struct, as the return value is never used. Hence why it's pointless that it would be compiled anyway. That's incorrect, it doesn't have anything to do with the

Re: returning struct, destructor

2016-12-21 Thread bauss via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 15:01:20 UTC, Eugene Wissner wrote: On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints

Re: returning struct, destructor

2016-12-21 Thread kinke via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 15:01:20 UTC, Eugene Wissner wrote: On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints

Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do

Re: returning struct, destructor

2016-12-21 Thread evilrat via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 14:15:06 UTC, John C wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do

Re: returning struct, destructor

2016-12-21 Thread John C via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: This prints 3 times "Destruct" with dmd 0.072.1. If I remove the if block, it prints "Destruct" only 2 times - the behavior I'm expecting. Why? Possibly to do with named return value optimisation.

Re: returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 12:32:51 UTC, Nicholas Wilson wrote: On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A

Re: returning struct, destructor

2016-12-21 Thread Nicholas Wilson via Digitalmars-d-learn
On Wednesday, 21 December 2016 at 11:45:18 UTC, Eugene Wissner wrote: Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A myFunc() { auto a = A(), b = A(); if (false) { return a;

returning struct, destructor

2016-12-21 Thread Eugene Wissner via Digitalmars-d-learn
Consider we have a function that returns a struct. So for example: import std.stdio; struct A { ~this() { writeln("Destruct"); } } A myFunc() { auto a = A(), b = A(); if (false) { return a; } return b; } void main() { myFunc(); } This prints 3

Struct destructor in a with block

2015-02-02 Thread Tofu Ninja via Digitalmars-d-learn
module main; import std.stdio; void main(string[] args) { with(test()) { foo(); } } struct test { void foo() { writeln(foo); } ~this() { writeln(destoy); } } prints:

Re: Struct destructor in a with block

2015-02-02 Thread Tofu Ninja via Digitalmars-d-learn
On Tuesday, 3 February 2015 at 05:09:55 UTC, Ali Çehreli wrote: Yes, it's a known bug that has been fixed on git head but I can't find the bug report. :-/ Ok cool, good to know. The new output: foo destoy Yes, without the 'r'. ;) Ali Yeah, i noticed the typo right after I posted...

Re: Struct destructor in a with block

2015-02-02 Thread Ali Çehreli via Digitalmars-d-learn
On 02/02/2015 07:51 PM, Tofu Ninja wrote: module main; import std.stdio; void main(string[] args) { with(test()) { foo(); } } struct test { void foo() { writeln(foo); } ~this() { writeln(destoy); } } prints: destroy

bug or not? with(mystruct()) { code; } calls struct destructor before code

2013-03-23 Thread J
Is this a bug? I'm very puzzled; it seems that the destructor for struct mystruct is called pre-maturely. It should be after the 'in call(): X 12, Y 0, Z 9 line, should it not? Using DMD64 D Compiler v2.062 on OSX 10.6.8 and Linux x86_64 Output is: $ dmd -run named14.d start of main in

Re: bug or not? with(mystruct()) { code; } calls struct destructor before code

2013-03-23 Thread Andrej Mitrovic
On 3/23/13, J not_av...@notavailable.com wrote: Is this a bug? It's a known bug: http://d.puremagic.com/issues/show_bug.cgi?id=8269

with statement doesn't call struct destructor?

2011-06-08 Thread simendsjo
import std.stdio; void main() { struct S { this(bool a) { writeln( this); } ~this() { writeln( ~this); } } writeln(scoped:); { auto s = S(true); } writeln(with:); with(S(true)) { } } Output: scoped: this ~this with: this

Re: with statement doesn't call struct destructor?

2011-06-08 Thread Jonathan M Davis
On 2011-06-08 13:31, simendsjo wrote: import std.stdio; void main() { struct S { this(bool a) { writeln( this); } ~this() { writeln( ~this); } } writeln(scoped:); { auto s = S(true); } writeln(with:); with(S(true)) { } } Output: scoped: this ~this with: this Report it

Re: with statement doesn't call struct destructor?

2011-06-08 Thread simendsjo
On 08.06.2011 23:14, Jonathan M Davis wrote: On 2011-06-08 13:31, simendsjo wrote: import std.stdio; void main() { struct S { this(bool a) { writeln( this); } ~this() { writeln( ~this); } } writeln(scoped:); { auto s = S(true); } writeln(with:); with(S(true)) { } } Output: scoped: this