On 10/28/2013 12:30 PM, Maxim Fomin wrote:

> So do you *know* cases or suspect that they may exists? Or remember some
> bug issue?
>
> Here is my attempt:
>
> import std.stdio;
>
> struct S
> {
>     int i;
>     this(int i)   { writefln("ctor, %X", i); this.i = i; }
>     this(this)  { writefln("postblit, %X, %X", &this, i); }
>     ~this()     { writefln("dtor, %X, %X", &this, i); }
> }
>
> auto foo()
> {
>     S s = S(1);
>     return { s = S(2); } ;
> }
>
> void main()
> {
>     foo()();
> }
>
> ctor, 1
> dtor, 7FFFF7ED8FF8, 1
> ctor, 2
> dtor, 7FFFFFFFDB30, 1
>
> Inside foo() function object 's' is destroyed twice: first time as a
> regular struct at the end of block scope, second time before assigning
> S(2).

Ok, that's too much! :( Inspired by your program, the following delegate completely misses the point:

import std.stdio;

struct S
{
    int i;
    ~this() { i = 666; }
}

auto foo()
{
    S s = S(1);

    // When I see the following delegate, I think "the lifetime of s is
    // extended." I expect my delegate to print 1.
    return { writeln(s); } ;
}

void main()
{
    // Unfortunately, the following prints 666!
    foo()();
}

What is the purpose of writeln in that delegate? Obviously, to print 1. Yet it doesn't happen that way. Is this accepted to be a bug? Should the programmer 'new' the object instead?

Ali

Reply via email to