Flexibly sized arrays (was Re: in-parameter)

Pillsy pillsbury at gmail.com
Mon Nov 8 11:26:36 PST 2010


Pillsy Wrote:
[...]
> So, wait, if I have a program like this:
 
> void appendSailor (string s) {
>    s ~= "Sailor";
> }
 
> void main () {
>    auto s = "Hello World!";
> 
>    appendSailor(s[0 .. 6]);
> 
>    writefln(s);
> }
 
> I should expect to get "Hello Sailor" as output? Or is it just that a 
> new array of characters will be allocated and that will be appended 
> into, so `appendSailor()` becomes a slightly expensive no-op?

No, wait, I'm a moron.

Having

s = "foo"
s ~= "bar"

mean that `s` now holds "foobar" is obviously pretty useful. But as useful as it is, I assume it doesn't mean manipulating the length freely, which is what concerns me. Since strings are immutable arrays, the question of what structure is being shared is mostly academic, and when it's not academic, it's a performance issue.

Cheers,
Pillsy


More information about the Digitalmars-d mailing list