array cast should be supported corrected

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 7 07:54:06 PDT 2008


"BCS" <ao at pathlink.com> wrote in message 
news:55391cb3300e68cac5e93619c98c at news.digitalmars.com...
> Reply to Bill,
>
>> On Thu, Aug 7, 2008 at 6:06 AM, BCS <ao at pathlink.com> wrote:
>>
>>> Reply to Frank,
>>>
>>>> However. What is better in a template arraycast with o(n) or a cast?
>>>> The programmer always needs to know what the concequences are. With
>>>> this argumentation the string concatenation ~ should also be banned
>>>> from
>>>> the language, because hidden heap allocation has always unbound
>>>> execution
>>>> time.
>>> Array cat's /always/ do a O(?) allocation and a O(n+m) copy.
>>>
>> Not true for ~=.  Sometimes allocates sometimes doesn't.  You'll have
>> to come up with a better argument than that. :-)
>>
>
> http://www.digitalmars.com/d/1.0/arrays.html
> "Concatenation always creates a copy of its operands"
>
> http://www.digitalmars.com/d/1.0/expression.html
> ""
> Assignment operator expressions, such as:  a op= b
> are semantically equivalent to:  a = a op b
> ""


I once thought as you do.  However, there is a conflicting statement later 
in that page:

http://www.digitalmars.com/d/1.0/arrays.html#resize

Although it says that it applies to the ~ and ~= operator, in reality it 
only applies to the ~= operator, I think that is an error in the spec.

Go ahead and try it out:

char[] x = new char[1];
x[0] = 'a';
char[] y = x;
x ~= 'a';

assert(x.ptr == y.ptr);

y ~= 'b';

assert(x[1] == 'b');

// now the concatenation operator

y = y ~ 'c';

assert(y.ptr != x.ptr);

-Steve 





More information about the Digitalmars-d mailing list