Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 11, 2015 Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
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. |
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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.
|
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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); |
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | 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?
|
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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
|
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
|
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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 |
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
June 11, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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 |
June 18, 2015 Re: Encapsulate return value in scoped | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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?
|
Copyright © 1999-2021 by the D Language Foundation