Changing elements during foreach
Jonathan M Davis
jmdavisProg at gmx.com
Mon Oct 21 15:24:56 PDT 2013
On Monday, October 21, 2013 22:14:08 ixid wrote:
> What would be the issue/s with disallowing appending to slices?
> So you'd have to explicitly duplicate before you could append.
All arrays are slices. There is no difference between the two. It's just that
if you start appending to one, it will eventually no longer point to the same
block of memory as any other arrays that pointed to the block of memory that
it was pointing to. So, there's no way for the compiler to know whether you're
trying to append to an array that was created by slicing another array as
opposed to being directly allocated.
Also, disallowing appending to slices would be needlessly restrictive even if
you could have that restriction. There's nothing wrong whatsoever with
appending to a slice. It just means that you have to be aware that it may end
up reallocating and not be a slice of the same memory anymore. So, if you want
to avoid that, then don't append to the array. If you don't mind that it
reallocates for what your code is doing, then it's not a problem. All it means
is that you have to be aware that appending to an array may make it so that it
will reallocate. Whether that's good or bad depends on what you're doing.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list