Array as an argument, ambiguous behaviour.

Cooler kulkin at hotbox.ru
Thu Jan 30 07:49:34 PST 2014


On Thursday, 30 January 2014 at 15:29:50 UTC, Steven 
Schveighoffer wrote:
> On Thu, 30 Jan 2014 10:24:14 -0500, Cooler <kulkin at hotbox.ru> 
> wrote:
>
>> On Thursday, 30 January 2014 at 14:40:36 UTC, Dicebot wrote:
>>> On Thursday, 30 January 2014 at 13:42:53 UTC, Cooler wrote:
>>>> If I use fun2() I expect that fun2() will change the content 
>>>> of my array, and all changes I will see. If I don't want any 
>>>> change to my array, I will use fun1(). What should I want to 
>>>> use fun3()?
>>>
>>> For changes to content of array but not array itself.
>>
>> I agree. I just want that the case can be expressed in 
>> language syntax more obvious - something like "fun(int[] const 
>> x){}" to emphasize that I understand that fun() can change 
>> content of array, and cannot change the {pointer,size} pair.
>
> That's what fun(int[] x) does :)
>
> -Steve

Again...
void fun(int[] x){ x ~= 5; }
auto a = new int[10];
fun(a); // Can you predict the content of 'a'?

In your case:
void fun(int[] x){ x = [1, 2]; } // Compilation ok. 
Implementation's error.
The fun() implementer made error and think that caller will get 
new array. But it will get it only at runtime!
If we for example (just for example) have
void fun(int[] const x){ x = [1, 2]; } // Compilation error.


More information about the Digitalmars-d-learn mailing list