Idiomatic way to process const/immutable arrays as ranges
Jonathan M Davis
jmdavisProg at gmx.com
Mon Feb 11 09:27:49 PST 2013
On Monday, February 11, 2013 18:01:53 Dicebot wrote:
> On Monday, 11 February 2013 at 16:35:22 UTC, Steven Schveighoffer
>
> wrote:
> > in means "const scope". scope is a no-op, const makes the
> > array const, including the pointer length.
> >
> > 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.
>
> I know all of this. And I need guarantees that initial slice will
> always start at the very same point and will never be consumed by
> any part of this function. Thus, I do not need tail const and in
> is exactly what I want.
Actually, it sounds like tail-const is _exactly_ what you want. If the
function accepts const(string)[], then _nothing_ you do to that parameter will
alter the original array, and no deep copies will be performed. Making the
whole array const buys you _nothing_. You're just making the local variable
const, making it so that you can't use it as a range. It has _zero_ effect on
the original array either way. I really don't understand what you're trying to
gain by making the whole array const.
> But I see no reason why I can't copy
> slice pointers to create a new tail const range to consume.
>
> To sum up: I want to maintain full const for parameter
> range/slice but be able to create a tail const range from it
> every time I want to actually consume it. Without copying data
> itself.
If you want a tail-const slice, then just slice it.
auto newSlice = orig[];
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list