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

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 11 11:18:41 PDT 2016


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.

Question is, should `each`

1) take all inputs (ranges, arrays, other foreachables) by reference, or
2) take some inputs (like static arrays) by reference, or
3) take all inputs by value (current behavior)?

#1 would break code. Would probably need some deprecating and name 
shuffling to be acceptable. Would also need to make sure that this is 
actually the most desirable behavior.

#2 would probably create surprising corner cases. I don't think we can 
tell for sure if a range needs to be passed by reference in order to see 
updates to its elements. I'd be against this.

#3 may be a little surprising in how it doesn't affect value types (like 
static arrays). However, before switching to #1, you'd need to make sure 
that that one doesn't have worse corner cases. I don't see any deal 
breakers, but that doesn't mean they're not there ;)

You also have to see if changing to #1 is worth the effort. It would be 
an effort not only for the implementer, but also for the users who have 
to update all their code.


More information about the Digitalmars-d-learn mailing list