scope front vs [0]

vitamin vit at vit.vit
Wed Nov 11 19:06:34 UTC 2020


Or similar problem:

class Foo{}

struct Slice{
     Foo[] data;

     this(return scope Foo[] data)@safe {
         this.data = data;
     }
     Slice opSlice()@safe return scope{
         return Slice(data);
     }
     Foo opIndex(size_t i)@safe return scope{
         return data[i];
     }
}

void main()@safe{
     Foo foo;
     scope Slice slice = Slice([new Foo]);

     foo = slice[][0];		//OK, Why?
     foo = slice[].opIndex(0);	//Error: scope variable slice 
assigned to foo with longer lifetime
}


More information about the Digitalmars-d-learn mailing list