www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2070] New: DMD should allow easy creation of stack-allocated classes

reply d-bugmail puremagic.com writes:
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.


-- 
May 05 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2070






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());


-- 
Aug 08 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2070






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?


-- 
Aug 12 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2070






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.


-- 
Aug 12 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2070







 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.
 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 --
Aug 13 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2070







 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. --
Aug 15 2008
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 01 2010