passing static arrays to each! with a ref param [Re: Why can't static arrays be sorted?]

Jon Degenhardt via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 11 12:46:31 PDT 2016


On Tuesday, 11 October 2016 at 18:18:41 UTC, ag0aep6g wrote:
> On 10/11/2016 06:24 AM, Jon Degenhardt wrote:
>> The example I gave uses ref parameters. On the surface it 
>> would seem
>> reasonable to that passing a static array by ref would allow 
>> it to be
>> modified, without having to slice it first.
>
> Your ref parameters are only for the per-element operations. 
> You're not passing the array as a whole by reference. And you 
> can't, because `each` itself takes the whole range by copy.
>
> So, the by-ref increments themselves do work, but they're 
> applied to a copy of your original static array.
>
I see. Thanks for the explanation. I wasn't thinking it through 
properly. Also, I guess I had assumed that the intent was that 
each! be able to modify the elements, and therefore the whole 
array it would be pass by reference, but didn't consider it 
properly.

I'm not going to make any suggestions about whether the behavior 
should be changed. At some point when I get a bit of time I'll 
try to submit a documentation change to make the current behavior 
clearer.

--Jon


More information about the Digitalmars-d-learn mailing list