The analogue of "fill-pointer" in D

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 11:45:08 PDT 2015


On 05/18/2015 10:52 AM, Steven Schveighoffer wrote:

 > On 5/18/15 1:43 PM, Ali Çehreli wrote:

 >> void main()
 >> {
 >>      auto a = new int[20];
 >>      foo(a);
 >>      //can't now append to a
 >
 > Well, sure you can :)
 >
 > a ~= 5; // works fine
 >
 > But I understand you mean that an append to 'a' will reallocate
 >
 >> }
 >>
 >> void foo(const(int)[] p)
 >> {
 >>      p ~= 42;
 >> }

 > BTW, the way to prevent this is to do something like:
 >
 > a) dup p on append
 > b) const x = p; scope(exit) x.assumeSafeAppend();

Exactly! That recent discovery of mine made me come up with this 
guideline: "Never append to a parameter slice."

No, I may not follow that guideline myself but it makes sense to me:

 
http://forum.dlang.org/thread/mi21dq$l6l$1@digitalmars.com#post-mi739e:241v83:241:40digitalmars.com

Ali



More information about the Digitalmars-d-learn mailing list