October 11, 2016Re: passing static arrays to each! with a ref param [Re: Why can't static arrays be sorted?]
Posted in reply to Jon Degenhardt
On Tuesday, 11 October 2016 at 19:46:31 UTC, Jon Degenhardt wrote: > 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. > Another perspective where the current behavior could be confusing is that it is somewhat natural to assume that 'each' is the functional equivalent of foreach, and that they can be used interchangeably. However, for static arrays they cannot be.
Copyright © 1999-2017 by the D Language Foundation