Why string alias is invariant ?

Sergey Gromov snake.scaly at gmail.com
Thu Jan 31 09:58:02 PST 2008


Well thanks for explanation, all this really makes sense.  Though there are many benefits and drawbacks for different approaches which I can't keep track of.

Janice Caron Wrote:
> On Jan 30, 2008 11:03 PM, Sergey Gromov <snake.scaly at gmail.com> wrote:
> > You can't pass a dynamically created data into a function receiving string!
> 
> Again, correct. This is also a good thing. It allows copy-on-write to
> work correctly. For example, consider a function which lowercases a
> string. Since string is invariant, that means that when lowercasing
> something that is /already/ lowercase, the function is free to return
> the original string.

Yes, this works for string tokenizers for instance.  But doesn't quite
for lowercasing.  The already lowercase case is generally rare and requires
an additional pass to check, so you only benefit from this if you optimize
for memory.  And if you optimize for speed, you'd want lowercasing
in-place because you're obviously in the middle of processing data, and the
chances that this data is immutable are little.  Even if so, there's always .dup for you.

And of course there's no reason in having 'string' parameters in functions
like read(), listdir() etc. because their result never contains parts of the
arguments.

> > P.S.  Many thought mus be put into choosing a return type for a library function.  Because if it returns a unique copy of data it must be char[] so that i'm free to modify it.
> 
> Well, consider again the example of lowercasing a string to see why
> that is not so.

But I'm talking about always returning a unique copy of data.  What about
that listdir() ?  The returned names are obviously unique because they're
received from file system.  If you make them invariant, and I want to
uppercase them, I can't do that in-place because I'm not sure if they're
actually unique.  If you make them mutable but I need them as is, I still cannot count on them not to change because who knows where this
data is also used.

Maybe a contract should be added for standard library that if a function
returns a mutable array, it guarantees that this array can be modified
without side effects and therefore can be safely assumed unique.

SnakE



More information about the Digitalmars-d mailing list