Array as an argument, ambiguous behaviour.

Sergei Nosov sergei.nosov at gmail.com
Wed Jan 29 05:51:30 PST 2014


On Wednesday, 29 January 2014 at 13:15:30 UTC, Cooler wrote:
> On Wednesday, 29 January 2014 at 12:40:00 UTC, Tobias Pankrath 
> wrote:
>> On Wednesday, 29 January 2014 at 11:46:23 UTC, Cooler wrote:
>>> Thank you for detailed explanation. But the question is - "Is 
>>> that correct that language allows ambiguous behavior?"
>>
>> Where is it ambiguous?
>
> Ambiguity is here...
> When I call fun1() or fun2() I know the behavior directly from 
> function signature (read the comments in my first post). For 
> fun3() case the caller side don't know the behavior directly 
> from function signature. To know what will happen with array 
> "a", the caller must see to fun3() body.
> Ambiguity is - in first and second cases the caller knows what 
> happens with "a", but in third case the caller does not know 
> what happens with "a".

I believe you encounter an array reallocation.

If fun3 doesn't change the size of the array - you will see every 
change made by fun3 to the contents of `a` (but the `a` itself 
cannot be changed - only the contents). No other way around.

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.



More information about the Digitalmars-d-learn mailing list