digitalmars.D.bugs - auto scope problem
- Koroskin Denis (31/31) Dec 04 2007 I don't know whether it is a bug or is already known, but I haven't seen...
- Jarrett Billingsley (16/44) Dec 04 2007 What compiler are you using, 0.173? The 'auto' keyword was replaced wit...
- Koroskin Denis (25/40) Dec 04 2007 Of course no,
- Jarrett Billingsley (12/35) Dec 04 2007 I believe you've found a bug :\
- Matti Niemenmaa (7/21) Dec 04 2007 It's there for backwards compatibility. Before 'scope', one had to use e...
- Jarrett Billingsley (3/6) Dec 04 2007 I think it's more likely a case Walter forgot to change in the compiler....
I don't know whether it is a bug or is already known, but I haven't seen the discussion around here. This code works just as intended: ... { auto x = new SomeClass(); } // x is deleted here ... however, it doesn't work in loops: for(auto Iterator it = myVec.begin(); it.hasNext(); it.moveNext()) { // do something } An object is deleted right after first check (iterator.hasNext()). It doesn't work in both 1.x and 2.x compiler branches. Is this behaviour ok or not? My second question is actually a feature request (to Walter). I'd like to have a struct with trivial constructor and destructor. Like this: struct VectorInt { int size = 8; auto int[] array = new int[8]; }; It is ok to have such struct and it initializes all the variables. However, it lacks trivial destructor - the one that would delete an array upon going out of scope (destruction). Can we have one so it would delete objects, marked as "auto", please? This is needed in order to have an ability to create containers on stack (without initialization): { /* auto */ vector!(int) myVec; // initialized and has a capacity == 8 } // vector is going out of scope here, and an array is destroyed correctly
Dec 04 2007
"Koroskin Denis" <2korden+digitalmars gmail.com> wrote in message news:fj3jm3$5n4$1 digitalmars.com...I don't know whether it is a bug or is already known, but I haven't seen the discussion around here. This code works just as intended: ... { auto x = new SomeClass(); } // x is deleted here ...What compiler are you using, 0.173? The 'auto' keyword was replaced with the 'scope' keyword to perform RAII more than a year ago. I take it your full code looks like: void main() { auto x = new SomeClass(); } In which case, yes, x will be deleted after main returns but only because the GC is run at program termination. The 'auto' here is just triggering variable type inference and nothing more. To get RAII, use 'scope': scope x = new SomeClass();My second question is actually a feature request (to Walter). I'd like to have a struct with trivial constructor and destructor. Like this: struct VectorInt { int size = 8; auto int[] array = new int[8]; }; It is ok to have such struct and it initializes all the variables. However, it lacks trivial destructor - the one that would delete an array upon going out of scope (destruction). Can we have one so it would delete objects, marked as "auto", please? This is needed in order to have an ability to create containers on stack (without initialization): { /* auto */ vector!(int) myVec; // initialized and has a capacity == 8 } // vector is going out of scope here, and an array is destroyed correctlyPeople have asked for RAII to work for class/struct members too. Who knows if we'll get it. Walter has a way of shooting down features and then implementing them out of the blue a year later.
Dec 04 2007
Jarrett Billingsley Wrote:What compiler are you using, 0.173? The 'auto' keyword was replaced with the 'scope' keyword to perform RAII more than a year ago. I take it your full code looks like: void main() { auto x = new SomeClass(); } In which case, yes, x will be deleted after main returns but only because the GC is run at program termination. The 'auto' here is just triggering variable type inference and nothing more. To get RAII, use 'scope': scope x = new SomeClass();Of course no, class Test { public: this() { printf("constructed\n"); } ~this() { printf("deleted\n"); } } int main(char[][] args) { { auto/scope Test t = new Test(); } printf("exit main\n"); return 0; } Both auto and scope give just the same result: "constructed, deleted, exit main" About loop: it works now, however it didn't some time ago!
Dec 04 2007
"Koroskin Denis" <2korden+digitalmars gmail.com> wrote in message news:fj3pbo$ees$1 digitalmars.com...Of course no, class Test { public: this() { printf("constructed\n"); } ~this() { printf("deleted\n"); } } int main(char[][] args) { { auto/scope Test t = new Test(); } printf("exit main\n"); return 0; } Both auto and scope give just the same result: "constructed, deleted, exit main"I believe you've found a bug :\ If you use any of the following: scope Test t = new Test(); scope t = new Test(); auto Test t = new Test(); it uses RAII, but it shouldn't for the last case. Notice: Test t = new Test(); auto t = new Test(); _do not_ use RAII as expected. That "auto Test t" seems to be a bug. Probably been there for ages.
Dec 04 2007
Jarrett Billingsley wrote:If you use any of the following: scope Test t = new Test(); scope t = new Test(); auto Test t = new Test(); it uses RAII, but it shouldn't for the last case. Notice: Test t = new Test(); auto t = new Test(); _do not_ use RAII as expected. That "auto Test t" seems to be a bug. Probably been there for ages.It's there for backwards compatibility. Before 'scope', one had to use exactly that form to get RAII. Of course, it's arguable whether it should be kept, but I'm fairly sure it's intentional. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Dec 04 2007
"Matti Niemenmaa" <see_signature for.real.address> wrote in message news:fj4ecm$1jfh$1 digitalmars.com...Of course, it's arguable whether it should be kept, but I'm fairly sure it's intentional.I think it's more likely a case Walter forgot to change in the compiler.
Dec 04 2007