Why are fixed length arrays passed by value while variable are passed by reference?

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 18 08:50:46 PDT 2013


On Thu, 18 Apr 2013 11:26:22 -0400, Maxim Fomin <maxim at maxim-fomin.ru>  
wrote:

> On Thursday, 18 April 2013 at 13:37:45 UTC, ixid wrote:
>> I know this will not be changed, I just want to understand why it is as  
>> it is.
>>
>> My naive thought is that consistency is the best scheme and that  
>> everything should have been passed by value or everything by reference  
>> unless the user specifies otherwise.
>
> Classifying types as passed by value and passed by reference
> types is a little bit confusing sometimes)
>
> In D, like in C, everything is passed by value (unless parameter
> is specified as ref, also note that in C passing by reference is
> a simulation done by pointers). Confusion comes when there is
> data which points to another data. Learning how data types are
> constructed is better approach than classifying their categories.
> The former sometimes can explain what the latter cannot:
>
> http://dpaste.dzfl.pl/7744d11e
>
> AA array behaves like a reference type, but when it is reset to
> null, it acts like a value type. Contradiction is caused by
> complex data structes with mixed structs and pointers in AA
> implementation.

AA's are not passed by reference, they ARE a reference.  That reference is  
ALWAYS passed by value, unless ref is used.

The problem with AA's is that the special reference null mutates upon  
first addition to the AA, and never changes afterwards, unless  
reassigned.  It is always passed by value.

In fact, if default construction allowed allocating the initial reference  
(or null wasn't treated specially), we wouldn't have that problem.

-Steve


More information about the Digitalmars-d-learn mailing list