Array as an argument, ambiguous behaviour.

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


>> Again - stop consider current state of D implementation. 
>> Consider how we can make D better. I think fun3() push 
>> programmers to make errors.
>
> I think functions like void fun(int[] a){} are bug prone, 
> because you seem to change the length of the array inside the 
> function, or if you perform an append you modify a new memory 
> zone, but such changes are invisible from the caller. Some 
> times this is what you want, and in some cases this is a 
> programmer mistake (this bug happened to me several times).
>
> But I don't know what to change and if this situation can be 
> improved now. Perhaps all that's left to improve is to add 
> tests to a D lint that warns against this possible source of 
> bugs.
>
> Bye,
> bearophile

You wrote "this bug happened to me several times" :) This bug 
also happened with me :)
I know that everybody uses variant of fun(int[] a){} everywhere. 
I understand that nobody will rewrite it source code. I just want 
to follow rule "Correct things must be simple. Wrong things must 
be difficult." As an example we can mark such usage as 
"deprecated", to push all programmers use more error prone 
variants.


More information about the Digitalmars-d-learn mailing list