digitalmars.D - Proposal : mnemonic for start index for slices
- Temtaime (9/9) Aug 08 2015 Hi !
- Rikki Cattermole (10/19) Aug 08 2015 Ohhhhh the start value not the start index.
- Idan Arye (2/11) Aug 08 2015 auto sub = arr[idx + 123 * 10 .. $][0 .. 1];
- John Colvin (6/15) Aug 08 2015 That would be neat, but the thing is you can already do this:
- sigod (9/14) Aug 09 2015 Shouldn't it be always rewritten by compiler regardless
- Shammah Chancellor (6/15) Aug 08 2015 More sugar is more overhead for new people to learn just to
- ChangLong (3/12) Aug 08 2015 this will work but lack of array bundle check.
- Walter Bright (4/5) Aug 09 2015 It's a good suggestion, but the responses here pretty much cover the ter...
Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?
Aug 08 2015
On 9/08/2015 1:08 a.m., Temtaime wrote:Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?Ohhhhh the start value not the start index. Humm, I'm ok with this. It would pair and match with $ which means end in regex with ^ meaning start. Although I'm sure Walter will say no. import std.stdio; void main() { int[] values = [1, 2, 3]; writeln(values[0 .. ^ + 3]); }
Aug 08 2015
On Saturday, 8 August 2015 at 13:08:08 UTC, Temtaime wrote:Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?auto sub = arr[idx + 123 * 10 .. $][0 .. 1];
Aug 08 2015
On Saturday, 8 August 2015 at 13:08:08 UTC, Temtaime wrote:Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?That would be neat, but the thing is you can already do this: auto sub = arr[idx + 123 * 10 .. $][0 .. 1]; The only argument that might hold weight is that calculating opDollar and doing two index/slice operations might be relatively expensive for some user-defined type.
Aug 08 2015
On Saturday, 8 August 2015 at 13:42:03 UTC, John Colvin wrote:That would be neat, but the thing is you can already do this: auto sub = arr[idx + 123 * 10 .. $][0 .. 1]; The only argument that might hold weight is that calculating opDollar and doing two index/slice operations might be relatively expensive for some user-defined type.Shouldn't it be always rewritten by compiler regardless underlying type? auto sub = arr[idx + 123 * 10 .. $][0 .. 1]; -> auto index = idx + 123 * 10; auto sub = arr[index .. index + 1]; I mean, does it make sense to have unequal results for this 2 expressions?
Aug 09 2015
On Saturday, 8 August 2015 at 13:08:08 UTC, Temtaime wrote:Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?More sugar is more overhead for new people to learn just to understand code written in the language. Why don't you do: auto startIdx = idx + 123 * 10; auto sub = arr[startIdx .. startIdx + 1]; -Shammah
Aug 08 2015
On Saturday, 8 August 2015 at 13:08:08 UTC, Temtaime wrote:Hi ! I want to add some sugar to D : sometimes it's necessary to use complex start index. For example: auto sub = arr[idx + 123 * 10..idx + 123 * 10 + 1]; Proposal is to add a mnemonic for start index, for instance : Any ideas ? Should i try to create a DIP ?this will work but lack of array bundle check. auto sub = (&arr[idx + 123 * 10)[0..1] ;
Aug 08 2015
On 8/8/2015 6:08 AM, Temtaime wrote:Any ideas ? Should i try to create a DIP ?It's a good suggestion, but the responses here pretty much cover the territory adequately with existing features. I don't think a new feature for this is justified. But we're always looking for better ideas, so don't be discouraged!
Aug 09 2015