On Wednesday 19 January 2011 18:23:14 Jerry Quinn wrote: > Andrei Alexandrescu Wrote: > > On 1/19/11 6:53 PM, Jonathan M Davis wrote: > > > On Wednesday, January 19, 2011 15:33:16 Andrei Alexandrescu wrote: > > >> I'm consolidating some routines from std.string into std.array. They > > >> are specialized for operating on arrays, and include the likes of > > >> insert, remove, replace. > > >> > > >> One question is whether operations should be performed in place or on > > >> a > > > > >> copy. For example: > > So I guess vote stays unchanged :o). > > > > >> Thoughts? > > > > > > Haven't we been using the approach that string operations generally > > > make copies (in many cases slices) and marking functions that do it in > > > place with InPlace? > > > > Problem is, even though the example uses strings, the functions apply to > > all arrays. > > The big difference is operating on immutable arrays vs mutable ones. For > immutable arrays, you have to do copies. But mutable ones allow in-place > editing. If I'm working with mutable arrays of ints, I don't want to have > to type InPlace after every function and I *really* don't want the array > to be copied or efficiency will go down the tubes. > > Nor do I want to add Copy to every string operation. This might be an > argument to leave the string functions where they are. To a certain > extent, strings are special, even though they really aren't. > > Is it too ugly to contemplate algorithms doing in-place operations on > mutable arrays and return a copy instead for immutable ones?
I'd say that yes, it's too ugly to contemplate. The reason is simple: the behavior of the function then changes drastically depending on whether the array you give it is immutable or not. - Jonathan M Davis