Implicit conversion of concatenation result to immutable

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 1 22:35:57 UTC 2021


On 4/1/21 6:34 PM, H. S. Teoh wrote:
> On Thu, Apr 01, 2021 at 03:07:09PM -0700, Ali Çehreli via Digitalmars-d wrote:
>> On 4/1/21 2:59 PM, H. S. Teoh wrote:
> [...]
>>> One may argue that appending in general will reallocate, and once
>>> reallocated it will be unique, and there safe to implicitly convert
>>> to immutable.  However, in general we cannot guarantee this
>>
>> Yes, that's tricky for append because one of many slices does own the
>> potential bytes after the array and will append elements in there.
>> However, concatenation always makes a new array, right? I think the
>> result can be char[] in that case.
> [...]
> 
> If one of the arguments is an empty array, does concatenation allocate a
> new array anyway? Or does it simply return the other argument? (I don't
> know.)  If not, then we cannot make it implicitly convertible.

Yes, always an allocation. See point 5 here: 
https://dlang.org/spec/arrays.html#array-concatenation

-Steve


More information about the Digitalmars-d mailing list