Array as an argument, ambiguous behaviour.

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 30 09:18:55 PST 2014


On Thu, 30 Jan 2014 12:07:07 -0500, Cooler <kulkin at hotbox.ru> wrote:

> On Thursday, 30 January 2014 at 16:18:33 UTC, Steven Schveighoffer wrote:

>> void foo(int x)
>> {
>>    x = 5;
>> }
>>
>> "hey, why doesn't that work! Setting a parameter to another value  
>> should be illegal!"
>
> Difference is here.
> "void foo(int x){}" - the caller will NEVER see any change to 'x'.
> "void foo(int[] x){}" - the caller MAY or MAY NOT see changes to 'x'.

This is incorrect:

foo(int[] x){} - The caller will see changes to data 'x' references.

A slice is a reference type, it references a specific block of data. It's  
more akin to a pointer than an int.

I could change my example:

void foo(int *x)
{
    int n = 3;
    x = &n;
}

"hey, why doesn't x now point to 3? Should be illegal!"

-Steve


More information about the Digitalmars-d-learn mailing list