Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why destroy(this._value) on reassignment? #20

Closed
SimonN opened this issue Aug 23, 2018 · 2 comments
Closed

Why destroy(this._value) on reassignment? #20

SimonN opened this issue Aug 23, 2018 · 2 comments

Comments

@SimonN
Copy link
Contributor

SimonN commented Aug 23, 2018

I'm reading optional.d of the current master 9940ac3. I'm wondering about this code here, starting in line 136:

    void opAssign()(const None) if (isMutable!T) {
        if (!this.empty) {
            destroy(this._value);
            this._empty = true;
        }
    }

Why is the call to destroy here when T is a class? When I reassign one of the references (such as this Optional!T) to a given class object, I usually don't need to destroy the GC-ed class. It can even be harmful if there are other references to the same object:

#!/usr/bin/rdmd
import std.stdio;
import optional;

class C {
    int i;
    this(int ai) { i = ai; }
}

void main()
{
    C my = new C(3);
    Optional!C opt = some(my);
    writeln(my.i); // prints 3

    opt = none;
    writeln(my.i); // prints 0, but should print 3
}

Should this code print 0 or 3?

@aliak00
Copy link
Owner

aliak00 commented Aug 24, 2018

Ooh good catch! Will fix.

@SimonN
Copy link
Contributor Author

SimonN commented Aug 24, 2018

Thanks for the quick fix and release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants