Idiomatic way to process const/immutable arrays as ranges
Jonathan M Davis
jmdavisProg at gmx.com
Mon Feb 11 08:53:53 PST 2013
On Monday, February 11, 2013 11:35:22 Steven Schveighoffer wrote:
> in means "const scope". scope is a no-op, const makes the array const,
> including the pointer length.
I would point out that that may change in the future. scope is supposed to
stop references escaping in general but is only partially implemented
(currently only for delegates - and I'm not even sure that it's fully
implemented there). That being the case, using scope or in on an array could
have a major effect on what you can do with it in the future (e.g. returning a
slice of it would become illegal), which is one reason why I pretty much
always tell people never to use in. Too many people take a liking to in on the
theory that it's the opposite of out, but it hides the fact that scope is
involved, and a lot of code is going to break once scope is fully implemented.
in is one thing that I would _love_ to have eliminated from the language, but
it's a carryover from D1 that was badly translated to D2.
> const(T)[] means, the ELEMENTS are const, but the pointer and length can
> be changed. This makes it a valid input range.
>
> Since your function is making a copy of the data, this should be fine.
>
> Your explanation is difficult to understand, I'm basically going on what
> your code does. If you change "in string[]" to "const(string)[]", the
> function should compile.
The const(T)[] cannot alter the original array at all, so I concur with Steven
in that the complaints about not wanting to use tail-const make no sense.
Maybe this article will help clear things up:
http://dlang.org/d-array-article.html
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list