www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Encapsulate return value in scoped

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
Is there a way to encapsulate return value into scoped?

Say I have a function that returns a new object:

X new_x(T t...) {
     //Super complex input processing
     return new X(something);
}

And I want to encapsulate the result using scoped, is that 
possible? Can I just do:
     return scoped!X(something).
?

If I understand correctly, the data will be blitted, but the 
destructor will be called, so the returned object will be in 
invalid state.
Jun 11 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 08:48:22 UTC, Yuxuan Shui wrote:
 Is there a way to encapsulate return value into scoped?

 Say I have a function that returns a new object:

 X new_x(T t...) {
     //Super complex input processing
     return new X(something);
 }

 And I want to encapsulate the result using scoped, is that 
 possible? Can I just do:
     return scoped!X(something).
 ?

 If I understand correctly, the data will be blitted, but the 
 destructor will be called, so the returned object will be in 
 invalid state.
It's even weirder than I thought, this: import std.stdio, std.typecons; class A { int b; ~this() { writeln("Des"); } this(int x) { b=x; writeln("Cons"); } } auto x() { A x = scoped!A(10); writeln(x.b); writeln("Return x"); return x; } void main() { auto tx = x(); writeln("Return main"); } Produce output: Cons Des 0 Return x Return main Which I totally don't understand.
Jun 11 2015
parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Thu, 11 Jun 2015 09:01:04 +0000
Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:
 	A x = scoped!A(10);
use auto x = scoped!A(10);
Jun 11 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:
 On Thu, 11 Jun 2015 09:01:04 +0000
 Yuxuan Shui via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:
 	A x = scoped!A(10);
use auto x = scoped!A(10);
Thanks! Curious question, why doesn't compiler reject this code?
Jun 11 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/11/15 1:28 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:
 On Thu, 11 Jun 2015 09:01:04 +0000
 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
 wrote:
     A x = scoped!A(10);
use auto x = scoped!A(10);
Thanks! Curious question, why doesn't compiler reject this code?
Because scoped!A implicitly casts to A. -Steve
Jun 11 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer 
wrote:
 On 6/11/15 1:28 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:
 On Thu, 11 Jun 2015 09:01:04 +0000
 Yuxuan Shui via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:
    A x = scoped!A(10);
use auto x = scoped!A(10);
Thanks! Curious question, why doesn't compiler reject this code?
Because scoped!A implicitly casts to A. -Steve
Thanks! I just found that out myself. Learned 'alias this' in the process.
Jun 11 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/11/2015 11:43 AM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer wrote:
 On 6/11/15 1:28 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:
 On Thu, 11 Jun 2015 09:01:04 +0000
 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
 wrote:
    A x = scoped!A(10);
use auto x = scoped!A(10);
Thanks! Curious question, why doesn't compiler reject this code?
Because scoped!A implicitly casts to A. -Steve
Thanks! I just found that out myself. Learned 'alias this' in the process.
Shameless plug: :) http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped This issue is explained at the end of that section. Ali
Jun 11 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:
 On 06/11/2015 11:43 AM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 17:34:56 UTC, Steven 
 Schveighoffer wrote:
 On 6/11/15 1:28 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák 
 wrote:
 On Thu, 11 Jun 2015 09:01:04 +0000
 Yuxuan Shui via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:
   A x = scoped!A(10);
use auto x = scoped!A(10);
Thanks! Curious question, why doesn't compiler reject this code?
Because scoped!A implicitly casts to A. -Steve
Thanks! I just found that out myself. Learned 'alias this' in the process.
Shameless plug: :) http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped This issue is explained at the end of that section. Ali
Can you explain more about why the destructor is not called when returning a struct? Can't seem to find it in the document.
Jun 11 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/11/2015 12:51 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:
   http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped

 This issue is explained at the end of that section.

 Ali
Can you explain more about why the destructor is not called when returning a struct?
Are you asking in general or specific to scoped!C? In general, D has move semantics built into the language. It depends on whether the returned expression is an rvalue or an lvalue: rvalues are moved, lvalues are copied. And the destructor will not be called for a moved object. About returning scoped!C, I think it works: import std.stdio; import std.typecons; class C { ~this() { writeln("dtor"); } } auto foo() { auto c = scoped!C(); return c; } void main() { writeln("entering scope"); { writeln("calling"); auto s = foo(); writeln("returned"); } writeln("leaving scope"); } "dtor" is printed upon leaving the scope: entering scope calling returned dtor leaving scope Ali
Jun 11 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Thursday, 11 June 2015 at 21:38:59 UTC, Ali Çehreli wrote:
 On 06/11/2015 12:51 PM, Yuxuan Shui wrote:
 On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:
 [...]
Can you explain more about why the destructor is not called
when
 returning a struct?
Are you asking in general or specific to scoped!C? In general, D has move semantics built into the language. It depends on whether the returned expression is an rvalue or an lvalue: rvalues are moved, lvalues are copied. And the destructor will not be called for a moved object. About returning scoped!C, I think it works: import std.stdio; import std.typecons; class C { ~this() { writeln("dtor"); } } auto foo() { auto c = scoped!C(); return c; } void main() { writeln("entering scope"); { writeln("calling"); auto s = foo(); writeln("returned"); } writeln("leaving scope"); } "dtor" is printed upon leaving the scope: entering scope calling returned dtor leaving scope Ali
I just find out that the document of scoped says that "It's illegal to move a class instance even if you are sure there are no pointers to it. As such, it is illegal to move a scoped object." So this is not a solution?
Jun 18 2015
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/18/2015 04:53 PM, Yuxuan Shui wrote:

 On Thursday, 11 June 2015 at 21:38:59 UTC, Ali Çehreli wrote:
 About returning scoped!C, I think it works:
 I just find out that the document of scoped says that "It's
Thanks for fixing the typo there. (The documentations has lower case: "it's". ;) )
 illegal to
 move a class instance
Wait! You fixed that as well? :) The doc that I am looking at says "class reference":
 even if you are sure there are no pointers to it.
 As such, it is illegal to move a scoped object."

 So this is not a solution?
I guess not. :-/ Ali
Jun 18 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Friday, 19 June 2015 at 00:00:50 UTC, Ali Çehreli wrote:
 On 06/18/2015 04:53 PM, Yuxuan Shui wrote:

 On Thursday, 11 June 2015 at 21:38:59 UTC, Ali Çehreli wrote:
 About returning scoped!C, I think it works:
 I just find out that the document of scoped says that "It's
Thanks for fixing the typo there. (The documentations has lower case: "it's". ;) )
 illegal to
 move a class instance
Wait! You fixed that as well? :) The doc that I am looking at says "class reference":
Those are fixed by: https://github.com/D-Programming-Language/phobos/pull/3016
 even if you are sure there are no pointers to it.
 As such, it is illegal to move a scoped object."

 So this is not a solution?
I guess not. :-/ Ali
I don't understand. What problem can moving a scoped cause when there's no reference to its inner class?
Jun 18 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/18/2015 05:25 PM, Yuxuan Shui wrote:

 What problem can moving a scoped cause when there's
 no reference to its inner class?
I am curious as well. Could it be related to the object's 'monitor'? Perhaps some other code may have a reference to monitor? And that reference does not constitute as a reference to the object itself? I don't know. Ali
Jun 18 2015