Array as an argument, ambiguous behaviour.
Sergei Nosov
sergei.nosov at gmail.com
Wed Jan 29 09:55:31 PST 2014
On Wednesday, 29 January 2014 at 14:48:12 UTC, Cooler wrote:
>> If, however, in fun3 you change the size of the array - it may
>> reallocate. Like, if you're appending to `x` - it will
>> allocate a new array and make x point to it. Now `a` and `x`
>> point to distinct arrays. And any change you do using `x`
>> won't be seen by `a`. And, yes, this is the intended behavior.
>
> That exactly what am i asking for!
> You have to know the body of fun3() to predict what happened
> with 'a'.
> When I call fun() there are 3 intentions:
> 1. I want just send the contents of 'a' to fun(). It can be
> done by a.dup, or you must sure that fun() has 'in' qualifier
> for it's argument.
> 2. I want fun() change content of 'a' array. This can be done
> by ensuring fun() has 'ref' qualifier for it's argument.
> What intention should I have to call fun3()?
E.g. you want fun3 to change the content of the array, but you
don't want it to change the pointer.
Consider,
void fun2(ref int[] x)
{
x = null;
}
void fun3(int[] x)
{
x = null;
}
void main()
{
int[] a = [1, 2, 3];
fun3(a);
assert(a);
fun2(a);
assert(!a);
}
More information about the Digitalmars-d-learn
mailing list