Why can't static arrays be sorted?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 10 09:46:55 PDT 2016


On Monday, October 10, 2016 16:29:41 TheGag96 via Digitalmars-d-learn wrote:
> On Saturday, 8 October 2016 at 21:14:43 UTC, Jon Degenhardt wrote:
> > This distinction is a bit on the nuanced side. Is it behaving
> > as it should?
> >
> > --Jon
>
> I think so? It's not being modified in the second case because
> the array is being passed by value... "x" there is a reference to
> an element of the copy created to be passed to each(). I assume
> there's a good reason why ranges in general are passed by value
> into these functions -- except in this one case, the stuff inside
> range types copied when passed by value won't be whole arrays,
> I'm guessing.

Whether it's by value depends entirely on the type of the range. They're
passed around, and copying them has whatever semantics it has. In most
cases, it copies the state of the range but doesn't copy all of the elements
(e.g. that's what happens with a dynamic array, since it gets sliced). But
if a range is a class, then it's definitely a reference type.  The only way
to properly save the state of a range is to call save.

But passing by ref would make no sense at all with input ranges. It would
completely kill chaining them. Almost all range-based functions return
rvalues.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list