const challenge
Christopher Wright
dhasenan at gmail.com
Sun Feb 3 13:54:38 PST 2008
Janice Caron wrote:
> Not really. We could have two functions, cat and icat, whereby cat
> always returns a mutable, and icat always returns an invariant. The
> concatenation operator, by contrast, always returns the same type as
> its inputs.
The issue with the concatenation operator is that its operands need not
be the same type. You can use invariant(char)[] and char[], and
invariant(char)[] takes precedence.
I'm not sure how often this issue comes up.
If you have a buffer, you can append to it with ~=. You might want to
prepend something, though, and in that case, the current behavior will
be problematic.
If you want to prepend something to a string, but you want to build the
stuff you prepend...this seems like a marginal use case, more so than
the buffer use case. It's easy enough to do:
char[] build;
const(char)[] prepend = build;
str = prepend ~ build;
The workaround for a mutable buffer is:
char[] tmpBuffer = new char[buffer.length + prepend.length];
tmpBuffer[0..prepend.length] = prepend[];
tmpBuffer[prepend.length..$] = buffer[];
buffer = tmpBuffer;
That is extremely ugly.
More information about the Digitalmars-d
mailing list