www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal : mnemonic for start index for slices

reply "Temtaime" <temtaime gmail.com> writes:
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
next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
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
prev sibling next sibling parent "Idan Arye" <GenericNPC gmail.com> writes:
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
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
parent "sigod" <sigod.mail gmail.com> writes:
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
prev sibling next sibling parent "Shammah Chancellor" <shammah.chancellor gmail.com> writes:
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
prev sibling next sibling parent "ChangLong" <changlon gmail.com> writes:
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
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
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