www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - -dip1000 safe scope wrapper

reply vit <vit vit.vit> writes:
Hello,
Is it possible to create scope wrapper initialized by non default 
constructor with scope parameter?
something like this:

struct Wrapper{
     int* p;

     static Wrapper create(scope return int* p) safe{
         Wrapper w;
         w.p = p;
         return w;
     }

     /++ This doesn't work:
     this(scope return int* p) safe scope{
         this.p = p;
     }+/
}

void main() safe{
     scope int i;
     scope Wrapper w1 = Wrapper.create(&i);
     scope Wrapper w2 = Wrapper(&i);
}
Jun 28 2018
parent Chris M. <chrismohrfeld comcast.net> writes:
On Thursday, 28 June 2018 at 13:29:58 UTC, vit wrote:
 Hello,
 Is it possible to create scope wrapper initialized by non 
 default constructor with scope parameter?
 something like this:

 struct Wrapper{
     int* p;

     static Wrapper create(scope return int* p) safe{
         Wrapper w;
         w.p = p;
         return w;
     }

     /++ This doesn't work:
     this(scope return int* p) safe scope{
         this.p = p;
     }+/
 }

 void main() safe{
     scope int i;
     scope Wrapper w1 = Wrapper.create(&i);
     scope Wrapper w2 = Wrapper(&i);
 }
I'm not sure why create works in this case either. I get the following when I uncomment the constructor test.d(12): Error: scope variable p assigned to this with longer lifetime Is there a subtle difference I'm missing?
Jun 28 2018