How to pass static array to function not by value?

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 08:05:13 PST 2014


On 22.11.2014 20:30, ketmar via Digitalmars-d-learn wrote:
> On Sat, 22 Nov 2014 08:07:31 -0800
> "H. S. Teoh via Digitalmars-d-learn"
> <digitalmars-d-learn at puremagic.com>  wrote:
>
>> On Sat, Nov 22, 2014 at 05:57:30PM +0200, ketmar via Digitalmars-d-learn wrote:
>>> On Sat, 22 Nov 2014 15:45:51 +0000
>>> Eric via Digitalmars-d-learn<digitalmars-d-learn at puremagic.com>  wrote:
>>>
>>>> Maybe this is not so lame because change() can take
>>>> any length of static array.
>>>
>>>    void change (int[] arr) {
>>>      arr[1] = 42;
>>>    }
>>>
>>>    void main () {
>>>      int[$] a = [1, 2, 3];
>>>      change(a);
>>
>> This is actually really scary that static arrays implicitly decay to a
>> dynamic array slice. It can cause all sorts of problems with escaping
>> dangling references to local variables:
> i don't even want to argue with that. ;-) just 'cause i'm sure that you
> are right, and this code shouldn't compile at all without explicit
> slicing of `a`.
>
> but this is what we have now, and i bet it will stay with us, 'cause
> "changing that will break alot of correct D code". so if we can't
> annihilate it, let's use it! ;-)
Does it worth to make some compiler option that for example prohibits 
passing static array instead of dynamic one without slicing? Who has a 
lot of breakable correct D code doesn't use it, but others do if they want?


More information about the Digitalmars-d-learn mailing list