Why are fixed length arrays passed by value while variable are passed by reference?
Jacob Carlborg
doob at me.com
Thu Apr 18 07:15:13 PDT 2013
On 2013-04-18 15:37, 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.
>
> I have read a comment by Andrei that they tried making fixed length
> arrays into reference types but it did not work well. Did the current
> situation arise through the reality of language development or is there
> a reason for the inconsistency?
An array is represent using a struct with a pointer to the array data
and the length, like this:
struct Array
{
void* ptr;
size_t length;
}
The struct is passed by value, but since it contains a pointer to the
data it will be passed by reference. Note that if you do:
void foo (int[] a)
{
a ~= 3;
}
auto b = [3, 4];
foo(b);
The caller will not see the change made by "foo".
Don't know if this explanation helped you to understand.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list