Re: Encapsulate return value in scoped
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: http://dlang.org/phobos/std_typecons.html#.scoped 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?
Re: Encapsulate return value in scoped
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
Re: Encapsulate return value in scoped
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?
Re: Encapsulate return value in scoped
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: http://dlang.org/phobos/std_typecons.html#.scoped 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
Re: Encapsulate return value in scoped
On Thu, 11 Jun 2015 09:01:04 + Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: A x = scoped!A(10); use auto x = scoped!A(10);
Re: Encapsulate return value in scoped
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.
Re: Encapsulate return value in scoped
On Thursday, 11 June 2015 at 09:11:47 UTC, Daniel Kozák wrote: On Thu, 11 Jun 2015 09:01:04 + 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?
Re: Encapsulate return value in scoped
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 + 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
Re: Encapsulate return value in scoped
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 + 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.
Re: Encapsulate return value in scoped
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 + 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
Re: Encapsulate return value in scoped
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 + 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.