| Thread overview | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 11, 2013 Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Monday, 11 February 2013 at 14:46:55 UTC, Dicebot 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.
Try:
string func(string[] args)
without 'in'.
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Mon, 11 Feb 2013 09:46:54 -0500, Dicebot <m.strashun@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
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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@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.
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Monday, 11 February 2013 at 14:53:17 UTC, Namespace wrote:
> On Monday, 11 February 2013 at 14:46:55 UTC, Dicebot 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.
>
> Try:
> string func(string[] args)
> without 'in'.
I have "in" there for a reason. There is no rationale in removing it. My request is perfectly valid - creating a mutable range for immutable array traversal.
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | Dicebot:
> string func(in string[] args)
> {
> return args.join(" "); // compile error, args is not an input range
> }
join() should to able to join a const array of strings.
Bye,
bearophile
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 11-Feb-2013 18:46, Dicebot пишет: > 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 > } I might be wrong but you can slice it e.g.: args[].join(" "); > > 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. -- Dmitry Olshansky | |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Mon, 11 Feb 2013 10:26:40 -0500, Dicebot <m.strashun@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@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
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Monday, 11 February 2013 at 16:25:35 UTC, Dmitry Olshansky wrote:
> I might be wrong but you can slice it e.g.:
>
> args[].join(" ");
[], as well as [0.$] does return the very same qualified slice, have tried it.
| |||
February 11, 2013 Re: Idiomatic way to process const/immutable arrays as ranges | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Monday, February 11, 2013 17:42:13 Dicebot wrote:
> On Monday, 11 February 2013 at 16:25:35 UTC, Dmitry Olshansky
>
> wrote:
> > I might be wrong but you can slice it e.g.:
> >
> > args[].join(" ");
>
> [], as well as [0.$] does return the very same qualified slice, have tried it.
For arrays, [] returns a tail-const slice of the array. If it doesn't, it's a bug.
- Jonathan M Davis
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply