View mode: basic / threaded / horizontal-split · Log in · Help
March 31, 2008
Nested slicing
One last thing about dollar notation in array slicing.
Shouldn't the following piece of code generate a compile-time error?

import std.stdio;

int main()
{
    int[] outer = [0, 1, 2, 3, 4];
    int[] inner = [5, 4, 3, 2, 1];

    int[] slice = outer[0..inner[$-1]]; < Error: shadowing declaration  
__dollar is deprecated

    writefln(slice.length);

    return 0;
}
March 31, 2008
Re: Nested slicing
On 31/03/2008, Koroskin Denis <2korden+dmd@gmail.com> wrote:
> One last thing about dollar notation in array slicing.
>  Shouldn't the following piece of code generate a compile-time error?

I hope not.

(And even if it does, the error message should mention $, not
__dollar, since __dollar is undocumented).
March 31, 2008
Re: Nested slicing
On Mon, 31 Mar 2008 18:04:53 +0400, Janice Caron <caron800@googlemail.com>  
wrote:

> On 31/03/2008, Koroskin Denis <2korden+dmd@gmail.com> wrote:
>> One last thing about dollar notation in array slicing.
>>  Shouldn't the following piece of code generate a compile-time error?
>
> I hope not.
>
> (And even if it does, the error message should mention $, not
> __dollar, since __dollar is undocumented).

My bet it should. This is both confusing and could be a source of errors.
And it is a shadowing indeed.
March 31, 2008
Re: Nested slicing
Koroskin Denis Wrote:

> One last thing about dollar notation in array slicing.
> Shouldn't the following piece of code generate a compile-time error?
> 
> import std.stdio;
> 
> int main()
> {
>      int[] outer = [0, 1, 2, 3, 4];
>      int[] inner = [5, 4, 3, 2, 1];
> 
>      int[] slice = outer[0..inner[$-1]]; < Error: shadowing declaration  
> __dollar is deprecated
> 
>      writefln(slice.length);
> 
>      return 0;
> }


Absolutely!  Shadowed variable declarations can be a tough thing to spot when things aren't working right.  I'd just say to use outer.length or inner.length to resolve ambiguity.
April 01, 2008
Re: Nested slicing
On Mon, 31 Mar 2008 17:59:21 +0400, Koroskin Denis <2korden+dmd@gmail.com>  
wrote:

> One last thing about dollar notation in array slicing.
> Shouldn't the following piece of code generate a compile-time error?
>
> import std.stdio;
>
> int main()
> {
>      int[] outer = [0, 1, 2, 3, 4];
>      int[] inner = [5, 4, 3, 2, 1];
>
>      int[] slice = outer[0..inner[$-1]]; < Error: shadowing declaration  
> __dollar is deprecated
>
>      writefln(slice.length);
>
>      return 0;
> }


This code might be ok. How about this one:

import std.stdio;

int main()
{
    int[] inner = [0, 1, 2, 3, 4];
    int[] outer = [5, 4, 3, 2, 1];

    int[] slice = outer[$-1..inner[$-1]];	// now this _is_ shadowing!

    writefln(slice.length);

    return 0;
}

What do you bet it will yield?
April 01, 2008
Re: Nested slicing
On Tue, 01 Apr 2008 20:31:09 +0400, Koroskin Denis wrote:

> On Mon, 31 Mar 2008 17:59:21 +0400, Koroskin Denis <2korden+dmd@gmail.com>  
> wrote:
> 
>> One last thing about dollar notation in array slicing.
>> Shouldn't the following piece of code generate a compile-time error?
>>
>> import std.stdio;
>>
>> int main()
>> {
>>      int[] outer = [0, 1, 2, 3, 4];
>>      int[] inner = [5, 4, 3, 2, 1];
>>
>>      int[] slice = outer[0..inner[$-1]]; < Error: shadowing declaration  
>> __dollar is deprecated
>>
>>      writefln(slice.length);
>>
>>      return 0;
>> }
> 
> This code might be ok. How about this one:
> 
> import std.stdio;
> 
> int main()
> {
>      int[] inner = [0, 1, 2, 3, 4];
>      int[] outer = [5, 4, 3, 2, 1];
> 
>      int[] slice = outer[$-1..inner[$-1]];	// now this _is_ shadowing!
> 
>      writefln(slice.length);
> 
>      return 0;
> }

Is it any more shadowing than ...

   int[] slice = outer[outer.length-1 .. inner[inner.length-1]];

which is its long-hand equivalent? 


-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
Top | Discussion index | About this forum | D home