Idiomatic way to process const/immutable arrays as ranges
Steven Schveighoffer
schveiguy at yahoo.com
Mon Feb 11 08:35:22 PST 2013
On Mon, 11 Feb 2013 10:26:40 -0500, Dicebot <m.strashun at gmail.com> wrote:
> On Monday, 11 February 2013 at 15:06:26 UTC, Steven Schveighoffer wrote:
>> On Mon, 11 Feb 2013 09:46:54 -0500, Dicebot <m.strashun at gmail.com>
>> wrote:
>>
>>> This question is so stupid I am just struck with trying to chose the
>>> "good" solution.
>>>
>>> Consider this code:
>>>
>>> string func(in string[] args)
>>> {
>>> return args.join(" "); // compile error, args is not an input range
>>> }
>>>
>>> It is somewhat expected as you can hardly popFront on a const range.
>>> But then question is: how can I wrap const slice into a mutable range
>>> without copying data (using standard language/library solution)? Of
>>> course, I can do .dup.join, but there is no real need to do copy, even
>>> a shallow one.
>>
>> What you are looking for is a tail-const array:
>>
>> string func(const(string)[] args)
>>
>> This will allow traversal, and not allow modification of the elements.
>>
>> -Steve
>
> No, I do not look for tail const. I don't want to allow head-mutability
> for parameter, but I want to make a mutable slice-range for it later.
> That does not break any const guarantees and should be safely possible.
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.
-Steve
More information about the Digitalmars-d-learn
mailing list