On Sunday, September 25, 2011 03:18:29 Andrew Wiley wrote: > On Sun, Sep 25, 2011 at 1:45 AM, Jonathan M Davis <jmdavisp...@gmx.com> wrote: > > On Sunday, September 25, 2011 02:37:25 Nick Sabalausky wrote: > >> If anyone cares, I've put up a little thing about why it's best not to > >> use D's arrays as stacks. This is drawn from a direct experience a > >> few months ago. Figured if I fell into that trap then others might > >> too, so this could be helpful for some people. There's a > >> no-login-needed comments section already there. > >> > >> https://www.semitwist.com/articles/article/view/don-t-use-arrays-as-st > >> acks> > > Yeah. If you want to use an array for a stack, it really needs to be > > wrapped in a struct or class like you'd do in C++. Expanding it when > > the size of the array needs to increase to accomodate items pushed onto > > the stack is certainly much easier in D, but trying to use the array as > > a stack directly is definitely going to cause issues as you show in the > > article. > > > > - Jonathan M Davis > > Isn't this exactly what assumeSafeAppend is for?
Sure, you could do that, but simply concatenating to push and then slicing to pop doesn't work. You need to do something like use assumeSafeAppend or just treat the stack as being part of the array instead of the whole. And in either case, if the array isn't wrapped in a struct or class, you're asking for trouble. In the case where you're just using part of the array as a stack, you need something to keep track of which part of the array is currently the stack, so you need a wrapper, and if you're using assumeSafeAppend, then you need to guarantee that nowhere else has a reference to that array (otherwise it's _not_ safe to assume that it's safe to append), so you need a wrapper. In either case, you need a wrapper. - Jonathan M Davis