in-parameter

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 8 09:58:19 PST 2010


On Monday, November 08, 2010 08:43:20 Pillsy wrote:
> Jonathan M Davis Wrote:
> [...]
> 
> > So, if you alter the elements of that array, it alters the elements of
> > the array that was passed in. However, if you alter the arrays size,
> > causing it to have to re-allocate memory, then that array is going to
> > be pointing to a different block  of memory, and it will no longer
> > affect the original array.
> 
> This behavior, IMO, is a real misfeature. The length property of an array
> shouldn't be directly mutable, and you shouldn't be able to append onto
> the end of a dynamic array, because it can cause some surprising behavior
> and adds a lot of cruft to the interface in the form of, well, most of
> std.array. The ability to use an an array as a replacement for an
> ArrayList or std::vector clashes badly with the ability to use arrays as
> lightweight slices.
> 
> Since lightweight slices are such a win[1], and people coming from any of
> Java/C#/C++ will be used to using a library class if they need a flexible
> array to use as a list, I think separating the two concepts and moving the
> flexible array into a library would be a notable improvement to the
> language. Sure, it may surprise Pythonistas, but they'll have a lot to
> learn anyway, given how much lower level and more static D is as a
> language.
> 
> [1] From a "marketing" perspective, they're also great way to show off how
> using a GCed language can actually improve performance and memory use.
> 
> Cheers,
> Pillsy

Concatenation for arrays is huge, and given that strings are arrays, it's that 
much more important. I agree that messing with the size of array can be 
confusing, but in practice, I've never seen it be a problem. If you want to 
ensure that an array can't be altered, then either you pass it as const or dup 
it. If it has const or immutable elements (such as string does), then you don't 
even have to worry about that. As long as you realize that resizing an array 
_can_ cause re-allocation, then you don't resize an array that you don't want to 
re-allocate. And if you want to _force_ re-allocation, then you simply use dup 
or idup.

I really don't think that this is generally an issue, and if you want to avoid 
it, all you have to do is just not resize arrays, so I really don't think that 
this is much of a problem in reality. And I'd have to have arrays hamstringed by 
disallowing concatenation and the like. I just don't see any real benefit in 
trying to do so. What we have works quite well.

- Jonathan M Davis


More information about the Digitalmars-d mailing list