Thread overview
Proposal : mnemonic for start index for slices
Aug 08, 2015
Temtaime
Aug 08, 2015
Rikki Cattermole
Aug 08, 2015
Idan Arye
Aug 08, 2015
John Colvin
Aug 09, 2015
sigod
Aug 08, 2015
Shammah Chancellor
Aug 09, 2015
ChangLong
Aug 09, 2015
Walter Bright
August 08, 2015
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 :

auto sub = arr[idx + 123 * 10..# + 1]; // # == start index

# is for example. Maybe it can be @ or some other symbol.

Any ideas ? Should i try to create a DIP ?
August 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 :
>
> auto sub = arr[idx + 123 * 10..# + 1]; // # == start index
>
> # is for example. Maybe it can be @ or some other symbol.
>
> 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]);	
}
August 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 :
>
> auto sub = arr[idx + 123 * 10..# + 1]; // # == start index
>
> # is for example. Maybe it can be @ or some other symbol.
>
> Any ideas ? Should i try to create a DIP ?

auto sub = arr[idx + 123 * 10 .. $][0 .. 1];
August 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 :
>
> auto sub = arr[idx + 123 * 10..# + 1]; // # == start index
>
> # is for example. Maybe it can be @ or some other symbol.
>
> 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.
August 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 :
>
> auto sub = arr[idx + 123 * 10..# + 1]; // # == start index
>
> # is for example. Maybe it can be @ or some other symbol.
>
> 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
August 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 :
>
> auto sub = arr[idx + 123 * 10..# + 1]; // # == start index
>
> # is for example. Maybe it can be @ or some other symbol.
>
> 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] ;
August 09, 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!
August 09, 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?