Bug 2070 – DMD should allow easy creation of stack-allocated classes

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-05-05T13:16:00Z
Last change time
2015-06-09T05:15:20Z
Assigned to
nobody
Creator
schveiguy

Comments

Comment #0 by schveiguy — 2008-05-05T13:16:43Z
Currently, if I want to temporarily create a stack-allocated class, then pass it to a function, then delete it after the function is done, I have to do this: int f(C c) {...} int i; { scope c = new C(); i = f(c); } // c is destroyed It would be nice if the above code could be one line. Perhaps something like: int i = f(scope new C()); This is really nice for classes that are stream or filter-type classes. Generally one only creates the filter-type class to be used temporarily for a single function call. For instance, in Tango, one can iterate through the lines of a file by doing: foreach(line; new LineInput(new FileInput("file.txt"))) { // do something with line. } Both the anonymous LineInput and FileInput classes can be stack-allocated, saving a call to the heap. This example is probably not quintessential, as FileInput allocates heap space for buffering, but LineInput AFAIK does not. Other examples can be thought of that would require no heap allocations, thereby saving the performance hit of allocating heap data.
Comment #1 by shro8822 — 2008-08-08T12:51:36Z
another solution would be to make delete an expression, like post increment, that has it's effect after it's value is used. you then get to use: int i = f(delete new C());
Comment #2 by davidl — 2008-08-12T08:59:40Z
you misunderstand post expression. delete new C() will only give you an invalid pointer, if it really behaves like a post expression. 1. new C() -> push the pointer to the stack 2. delete C() -> delete the object 3. call the function it's not working in the way: 1. new C() 2. call the func 3. delete C() and from the spec, scope attribute is only for RAII http://www.digitalmars.com/d/1.0/attribute.html#scope "The scope attribute is used for local variables and for class declarations. For class declarations, the scope attribute creates a scope class. For local declarations, scope implements the RAII (Resource Acquisition Is Initialization) protocol. This means that the destructor for an object is automatically called when the reference to it goes out of scope. The destructor is called even if the scope is exited via a thrown exception, thus scope is used to guarantee cleanup." but from a quick test, it's on the stack! why the spec didn't mention this?
Comment #3 by 2korden — 2008-08-12T09:09:14Z
I think he was making a proposal. The same one, in fact, the only difference is "delete" keyword used instead of "scope". But foo(scope new Bar()) is far more intuitive than foo(delete new Bar()) to me.
Comment #4 by schveiguy — 2008-08-13T08:14:18Z
(In reply to comment #2) > and from the spec, scope attribute is only for RAII > http://www.digitalmars.com/d/1.0/attribute.html#scope > > "The scope attribute is used for local variables and for class declarations. > For class declarations, the scope attribute creates a scope class. For local > declarations, scope implements the RAII (Resource Acquisition Is > Initialization) protocol. This means that the destructor for an object is > automatically called when the reference to it goes out of scope. The destructor > is called even if the scope is exited via a thrown exception, thus scope is > used to guarantee cleanup." > > but from a quick test, it's on the stack! why the spec didn't mention this? It's in the spec. Go to http://www.digitalmars.com/d/2.0/expression.html and search for scope. (In reply to comment #3) > I think he was making a proposal. The same one, in fact, the only difference is > "delete" keyword used instead of "scope". > > But foo(scope new Bar()) is far more intuitive than foo(delete new Bar()) to > me. Me too. -Steve
Comment #5 by bruno.do.medeiros+deebugz — 2008-08-15T07:29:52Z
(In reply to comment #3) > I think he was making a proposal. The same one, in fact, the only difference is > "delete" keyword used instead of "scope". > But foo(scope new Bar()) is far more intuitive than foo(delete new Bar()) to > me. I think: new scope Bar()) might be even better. It makes 'scope' look more like a type attribute such as const/invariant. Might be a direction worth looking into, but it's a deeper change that needs more thinking.
Comment #6 by nfxjfg — 2010-06-01T22:55:27Z
According to Andrei, scope will be completely removed from D2: http://lists.puremagic.com/pipermail/digitalmars-d/2010-June/076766.html