Array operations, dynamic arrays and length

J Miller via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 2 12:27:56 PDT 2015


On Thursday, 2 July 2015 at 12:59:03 UTC, Steven Schveighoffer 
wrote:
> On 7/2/15 8:21 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>> On Thursday, 2 July 2015 at 10:48:56 UTC, Steven Schveighoffer 
>> wrote:
>>> On 7/1/15 8:36 PM, J Miller wrote:
>>>
>>>> Oh, and to make things really confusing, "auto e = a[] - 
>>>> b[]" and "int[]
>>>> e = a[] - b[]" both cause "Error: array operation a[] - b[] 
>>>> without
>>>> destination memory not allowed".
>>>>
>>>> Using dmd 2.067.0.
>>>
>>> This is not a bug. You need to allocate memory before you can 
>>> write to
>>> it. Automatic allocation doesn't happen in D.
>>
>> This particular part works as intended, but the other things 
>> _are_ buggy.
>
> Right, my point was to address an obvious misconception on how 
> these statements work. Other languages happily will allocate a 
> new array in such cases, D does not.
>
> -Steve

I knew that automatic allocation doesn't happen, but I'm confused 
by the fact if you explicitly declare "c" with "int[] c;" and 
then assign "c[] = a[] * b[]", versus using "auto c = a[] * b[]", 
you get two different errors (array length mismatch vs no 
destination memory).


More information about the Digitalmars-d-learn mailing list