March 30, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | Language ref -> Array -> Slice "An array slice does not copy the data, it is only another reference to it." So the total slice of a static array is a range using the underlying memory of the static array isnt it ? |
March 30, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | On Monday, 30 March 2015 at 19:15:25 UTC, matovitch wrote:
> Language ref -> Array -> Slice
> "An array slice does not copy the data, it is only another reference to it."
>
> So the total slice of a static array is a range using the underlying memory of the static array isnt it ?
yes
|
March 30, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | Thanks. On a sader note, I found a respons'less thread about my second question : http://forum.dlang.org/thread/mailman.2247.1353945423.5162.digitalmars-d-learn@puremagic.com "where std.container.Array is concerned: how come I can't use a foreach(i, x; myArray) formulation? I.e. one where the foreach can infer the index value as well as the contained value ..." |
March 30, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch | On Monday, 30 March 2015 at 19:03:05 UTC, matovitch wrote:
> Well I have a bit of a similar problem with foreach.
>
> If I use classic T[] range, I can do :
>
> foreach(int i, auto t, myRange)...
>
> But if I use an Array!T (from std.container) I get :
>
> cannot infer argument types, expected 1 argument, not 2
>
> Even if I add the brackets []. Any idea ? Thanks for your help ! :)
The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly.
foreach(i, t; myRange.enumerate) {...}
size_t i = 0;
foreach(t; myRange) {... ++i;}
|
March 30, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Monday, 30 March 2015 at 19:31:54 UTC, anonymous wrote:
> On Monday, 30 March 2015 at 19:03:05 UTC, matovitch wrote:
>> Well I have a bit of a similar problem with foreach.
>>
>> If I use classic T[] range, I can do :
>>
>> foreach(int i, auto t, myRange)...
>>
>> But if I use an Array!T (from std.container) I get :
>>
>> cannot infer argument types, expected 1 argument, not 2
>>
>> Even if I add the brackets []. Any idea ? Thanks for your help ! :)
>
> The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly.
>
> foreach(i, t; myRange.enumerate) {...}
>
> size_t i = 0;
> foreach(t; myRange) {... ++i;}
Is it a compiler problem or a language restriction ? Thanks for the workaround btw, I will try it !
|
March 31, 2015 Re: Mapping with partial | ||||
---|---|---|---|---|
| ||||
Posted in reply to matovitch Attachments: | On Mon, 30 Mar 2015 19:36:49 +0000, matovitch wrote:
>> The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly.
>>
>> foreach(i, t; myRange.enumerate) {...}
>>
>> size_t i = 0;
>> foreach(t; myRange) {... ++i;}
>
> Is it a compiler problem or a language restriction ?
it is by design. `foreach` is not a "fancy for with hidden counter", it's more high-level construct. so if range isn't providing the counter, `foreach` will not guess why, and simply doesn't provide it too.
|
Copyright © 1999-2021 by the D Language Foundation