www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ptr wrapper with dip1000

reply vit <vit vit.vit> writes:
How to manualy declare constructor for struct Ptr which work like 
Ptr.create?

struct Ptr{
     int* ptr;

     static Ptr create(scope return int* ptr) safe{
         Ptr x;
         x.ptr = ptr;
         return x;
     }

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

void main() safe{
     int i;
     auto x = Ptr(&i);
     auto y = Ptr.create(&i);
}
Dec 19 2017
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/19/17 8:22 AM, vit wrote:
 struct Ptr{
      int* ptr;
 
      static Ptr create(scope return int* ptr) safe{
          Ptr x;
          x.ptr = ptr;
          return x;
      }
 
      /++ This doesn't work:
      this(scope return int* ptr)scope  safe{
          this.ptr = ptr;
      }
      +/
 }
 
 void main() safe{
      int i;
      auto x = Ptr(&i);
      auto y = Ptr.create(&i);
 }
I think this is a limitation of dip1000 that was not foreseen. I think when you mark the parameter `return`, `this` isn't actually considered a return value, is it? So it doesn't link the lifetime of ptr to the lifetime of this, which it should. Please file an enhancement request, this should be allowed. -Steve
Dec 19 2017