www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Assigning a scope variable to an outer-scope variable is allowed?

reply =?UTF-8?B?IuOBruOBl+OBhOOBiyAobm9zaGlpa2EpIg==?= writes:
Hello. I have a question about "scope" variables.

I've understood that it is not allowed to copy a scope variable or a 
scope parameter to outside of the scope. But, the following code 
compiles without any warnings or errors, and of course, causes a runtime 
error.

Is this just a bug?

//----

class C {
     ~this() { writefln("dtor"); }
     void func() { }
}
void main() {
     C a;
     {   scope C b = new C;
         a = b;
     } // dtor
     if(a !is null) a.func(); // Error: Access Violation
}

const(int[]) g;
void foo(in int[] a) { // in == final const scope
     g = a;  // a is scope?
}
Jul 04 2007
parent reply Gregor Richards <Richards codu.org> writes:
のしいか (noshiika) wrote:
 Hello. I have a question about "scope" variables.
 
 I've understood that it is not allowed to copy a scope variable or a 
 scope parameter to outside of the scope. But, the following code 
 compiles without any warnings or errors, and of course, causes a runtime 
 error.
 
 Is this just a bug?
 
 //----
 
 class C {
     ~this() { writefln("dtor"); }
     void func() { }
 }
 void main() {
     C a;
     {   scope C b = new C;
         a = b;
     } // dtor
     if(a !is null) a.func(); // Error: Access Violation
 }
 
 const(int[]) g;
 void foo(in int[] a) { // in == final const scope
     g = a;  // a is scope?
 }

Assigning a scope object to a variable which is not in that scope is valid, it's just usually stupid. One use where it's necessary (though a bit unclean) is if you have a global variable which you want to temporarily set to a scope object. You set it at the beginning of your scope, then reset it at the end, so it's never referring to an invalid object. That is to say: You're right, that's not allowed. But it's not allowed due to it being impossible at /runtime/, not any problems at /compile/ time. And adding a warning would make legit uses a PITA. - Gregor Richards
Jul 04 2007
parent reply =?UTF-8?B?IuOBruOBl+OBhOOBiyAobm9zaGlpa2EpIg==?= writes:
Gregor Richards wrote:
 const(int[]) g;
 void foo(in int[] a) { // in == final const scope
     g = a;  // a is scope?
 }

Assigning a scope object to a variable which is not in that scope is valid, it's just usually stupid. One use where it's necessary (though a bit unclean) is if you have a global variable which you want to temporarily set to a scope object. You set it at the beginning of your scope, then reset it at the end, so it's never referring to an invalid object. That is to say: You're right, that's not allowed. But it's not allowed due to it being impossible at /runtime/, not any problems at /compile/ time. And adding a warning would make legit uses a PITA.

Well, then isn't it inconsistent with what Walter said before? news://news.digitalmars.com:119/f2iism$tco$1 digitalmars.com Walter Bright wrote:
 scope - the function will not keep a reference to the parameter's data
 that will persist beyond the scope of the function

 For example:
 int[] g;

 void foo(in int[] a)
 {
     a = [1,2];    // error, a is final
     a[1] = 2;   // error, a is const
     g = a;    // error, a is scope
 }

Jul 07 2007
parent Leonard Dahlmann <leo.dahlmann gmail.com> writes:
This just isn't implemented yet.

&#65533;&#65533;&#65533;&#65533; (noshiika) Wrote:
 Well, then isn't it inconsistent with what Walter said before?

Jul 08 2007