Summary: Rebinding of scoped class references
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-05-20 13:25:35 PDT ---
This is a D2 program:

import std.stdio: printf;
class Foo {
    int x;
    this(int xx) { this.x = xx; }
    ~this() { printf("Foo(%d) destructor\n", this.x); }
void main() {
    scope Foo f1 = new Foo(1);
    Foo f2 = new Foo(2);
    f1 = f2;

With DMD v2.046 it compiles and at runtime it prints just:
Foo(2) destructor

If the Foo(1) object was meant to be deallocated at the end of the scope of
main(), this program can cause problems.

I can see two solutions:

1) Disallow the overwriting (rebinding) of references of scoped objects. This
can be acceptable.

2) The right semantics of the code I have written there seems that both F(1)
and F(2) objects have to be allocated with main() ends.
So the compiler can look if inside main() the f1 is written over. If it's never
written over in main(), then it can act normally.
Otherwise at runtime at the end of the scope of main() the runtime can look at
the f1 reference, if it refers to the object actually allocated on the stack,
then it can act normally. If it points elsewhere then the runtime can call the
destructor of both the object on the stack and the object referenced. I am not
sure if this can be done.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to