www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Nested slicing

reply "Koroskin Denis" <2korden+dmd gmail.com> writes:
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 =3D [0, 1, 2, 3, 4];
     int[] inner =3D [5, 4, 3, 2, 1];

     int[] slice =3D outer[0..inner[$-1]]; < Error: shadowing declaratio=
n  =

__dollar is deprecated

     writefln(slice.length);

     return 0;
}
Mar 31 2008
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
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).
Mar 31 2008
parent "Koroskin Denis" <2korden+dmd gmail.com> writes:
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.
Mar 31 2008
prev sibling next sibling parent Jason House <jason.james.house gmail.com> writes:
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.
Mar 31 2008
prev sibling parent reply "Koroskin Denis" <2korden+dmd gmail.com> writes:
On Mon, 31 Mar 2008 17:59:21 +0400, Koroskin Denis <2korden+dmd gmail.co=
m>  =

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 =3D [0, 1, 2, 3, 4];
      int[] inner =3D [5, 4, 3, 2, 1];

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

      writefln(slice.length);

      return 0;
 }
This code might be ok. How about this one: import std.stdio; int main() { int[] inner =3D [0, 1, 2, 3, 4]; int[] outer =3D [5, 4, 3, 2, 1]; int[] slice =3D outer[$-1..inner[$-1]]; // now this _is_ shadowing!= writefln(slice.length); return 0; } What do you bet it will yield?
Apr 01 2008
parent Derek Parnell <derek psych.ward> writes:
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
Apr 01 2008