Thread overview
[Issue 2070] New: DMD should allow easy creation of stack-allocated classes
May 05, 2008
d-bugmail
Aug 08, 2008
d-bugmail
Aug 12, 2008
d-bugmail
Aug 12, 2008
d-bugmail
Aug 13, 2008
d-bugmail
Aug 15, 2008
d-bugmail
Jun 02, 2010
nfxjfg@gmail.com
May 05, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070

           Summary: DMD should allow easy creation of stack-allocated
                    classes
           Product: D
           Version: 1.029
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: schveiguy@yahoo.com


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.


-- 

August 08, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070





------- Comment #1 from shro8822@vandals.uidaho.edu  2008-08-08 12:51 -------
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());


-- 

August 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070





------- Comment #2 from davidl@126.com  2008-08-12 08:59 -------
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?


-- 

August 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070





------- Comment #3 from 2korden@gmail.com  2008-08-12 09:09 -------
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.


-- 

August 13, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070





------- Comment #4 from schveiguy@yahoo.com  2008-08-13 08:14 -------
(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


-- 

August 15, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2070





------- Comment #5 from brunodomedeiros+bugz@gmail.com  2008-08-15 07:29 -------
(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.


-- 

June 02, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2070


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |nfxjfg@gmail.com
         Resolution|                            |WONTFIX


--- Comment #6 from nfxjfg@gmail.com 2010-06-01 22:55:27 PDT ---
According to Andrei, scope will be completely removed from D2: http://lists.puremagic.com/pipermail/digitalmars-d/2010-June/076766.html

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------