digitalmars.D.learn - Encapsulate return value in scoped
- Yuxuan Shui (13/13) Jun 11 2015 Is there a way to encapsulate return value into scoped?
- Yuxuan Shui (31/44) Jun 11 2015 It's even weirder than I thought, this:
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/5) Jun 11 2015 On Thu, 11 Jun 2015 09:01:04 +0000
- Yuxuan Shui (3/9) Jun 11 2015 Thanks!
- Steven Schveighoffer (3/13) Jun 11 2015 Because scoped!A implicitly casts to A.
- Yuxuan Shui (4/20) Jun 11 2015 Thanks!
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/25) Jun 11 2015 Shameless plug: :)
- Yuxuan Shui (4/35) Jun 11 2015 Can you explain more about why the destructor is not called when
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (38/46) Jun 11 2015 Are you asking in general or specific to scoped!C?
- Yuxuan Shui (6/50) Jun 18 2015 I just find out that the document of scoped says that "It's
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/16) Jun 18 2015 Thanks for fixing the typo there. (The documentations has lower case:
- Yuxuan Shui (5/22) Jun 18 2015 Those are fixed by:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/8) Jun 18 2015 I am curious as well. Could it be related to the object's 'monitor'?
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
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
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
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:Thanks! Curious question, why doesn't compiler reject this code?A x = scoped!A(10);use auto x = scoped!A(10);
Jun 11 2015
On 6/11/15 1:28 PM, Yuxuan Shui wrote:On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:Because scoped!A implicitly casts to A. -SteveOn Thu, 11 Jun 2015 09:01:04 +0000 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks! Curious question, why doesn't compiler reject this code?A x = scoped!A(10);use auto x = scoped!A(10);
Jun 11 2015
On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer wrote:On 6/11/15 1:28 PM, Yuxuan Shui wrote:Thanks! I just found that out myself. Learned 'alias this' in the process.On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:Because scoped!A implicitly casts to A. -SteveOn Thu, 11 Jun 2015 09:01:04 +0000 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks! Curious question, why doesn't compiler reject this code?A x = scoped!A(10);use auto x = scoped!A(10);
Jun 11 2015
On 06/11/2015 11:43 AM, Yuxuan Shui wrote:On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer wrote:Shameless plug: :) http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped This issue is explained at the end of that section. AliOn 6/11/15 1:28 PM, Yuxuan Shui wrote:Thanks! I just found that out myself. Learned 'alias this' in the process.On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:Because scoped!A implicitly casts to A. -SteveOn Thu, 11 Jun 2015 09:01:04 +0000 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks! Curious question, why doesn't compiler reject this code?A x = scoped!A(10);use auto x = scoped!A(10);
Jun 11 2015
On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:On 06/11/2015 11:43 AM, Yuxuan Shui wrote:Can you explain more about why the destructor is not called when returning a struct? Can't seem to find it in the document.On Thursday, 11 June 2015 at 17:34:56 UTC, Steven Schveighoffer wrote:Shameless plug: :) http://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped This issue is explained at the end of that section. AliOn 6/11/15 1:28 PM, Yuxuan Shui wrote:Thanks! I just found that out myself. Learned 'alias this' in the process.On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote:Because scoped!A implicitly casts to A. -SteveOn Thu, 11 Jun 2015 09:01:04 +0000 Yuxuan Shui via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks! Curious question, why doesn't compiler reject this code?A x = scoped!A(10);use auto x = scoped!A(10);
Jun 11 2015
On 06/11/2015 12:51 PM, Yuxuan Shui wrote:On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote: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 Alihttp://ddili.org/ders/d.en/destroy.html#ix_destroy.scoped This issue is explained at the end of that section. AliCan you explain more about why the destructor is not called when returning a struct?
Jun 11 2015
On Thursday, 11 June 2015 at 21:38:59 UTC, Ali Çehreli wrote:On 06/11/2015 12:51 PM, Yuxuan Shui wrote: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?On Thursday, 11 June 2015 at 19:23:49 UTC, Ali Çehreli wrote:when[...]Can you explain more about why the destructor is not calledreturning 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 18 2015
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'sThanks for fixing the typo there. (The documentations has lower case: "it's". ;) )illegal to move a class instanceWait! 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
On Friday, 19 June 2015 at 00:00:50 UTC, Ali Çehreli wrote:On 06/18/2015 04:53 PM, Yuxuan Shui wrote:Those are fixed by: https://github.com/D-Programming-Language/phobos/pull/3016On 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'sThanks for fixing the typo there. (The documentations has lower case: "it's". ;) )illegal to move a class instanceWait! You fixed that as well? :) The doc that I am looking at says "class reference":I don't understand. What problem can moving a scoped cause when there's no reference to its inner class?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
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