www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - symbolless stack allocation

reply "Jay" <jaypinkman hotmail.com> writes:
as specified here: 
http://wiki.dlang.org/Memory_Management#Allocating_Class_Instances_On_The_Stack

i can allocate a class instance on the stack (inside a funtion) 
like so:

scope c = new C();

i want to create an instance, call one of its methods, and throw 
it away. like so:

scope new C().doSomething();

can i somehow do away with declaring a local variable and calling 
a method on it?
Sep 10 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/10/2014 11:31 AM, Jay wrote:

 as specified here:
 
http://wiki.dlang.org/Memory_Management#Allocating_Class_Instances_On_The_Stack
 i can allocate a class instance on the stack (inside a funtion) like so:

 scope c = new C();
That usage of 'scope' is deprecated. There is std.typecons.scoped instead.
 i want to create an instance, call one of its methods, and throw it
 away. like so:

 scope new C().doSomething();

 can i somehow do away with declaring a local variable and calling a
 method on it?
Yes. import std.stdio; import std.typecons; class C { int i; this (int i) { this.i = i; } void foo() { writeln(i); } } void main() { scoped!C(42).foo(); } Ali
Sep 10 2014
parent "Jay" <jaypinkman hotmail.com> writes:
On Wednesday, 10 September 2014 at 19:39:17 UTC, Ali Çehreli 
wrote:

 scope c = new C();
That usage of 'scope' is deprecated. There is std.typecons.scoped instead.
actually i read about 'scoped' here http://ddili.org/ders/d.en/destroy.html (thanks for the tutorial btw) but i thought that 'scoped() wraps the class object inside a struct' means that the struct contains a reference to a heap allocated object. i guess i should've looked up the documentation for 'scoped'. thanks.
Sep 10 2014