Lets deprecate the length in the arrays index
Lionello Lunesu
lio at lunesu.remove.com
Mon Jan 15 08:40:10 PST 2007
Frank Benoit (keinfarbton) wrote:
> There was this thread:
>
> 22-Oct-2005
> It's time to deprecate "array [length]" in favour of "array [$]"
>
> Actually I noticed, i still can use length inside an array index, even
> if compiling without -d (deprecated).
>
> For me that means, I do not use the identifier 'length' in D. For
> nothing. Because it can happen, it collides with this array stranger.
>
> Please remove this or deprecated this.
I'd like the special case "length" to be removed as much as the next
guy, but actually, I think it would be nicer to generalize it: why not
let the brackets [..] be treated as an implicit with()? I mean, inside
the brackets you have the context of the thing before the brackets:
array[identifier];
//=>
with(array)
opIndex(identifier);
//and
array[ident1..ident2];
//=>
with(array)
opSlice(ident1,ident2);
(ATM, 'with' only accepts class objects)
This gets rid of the special case and makes it available to class object:
import std.stdio;
class X {
uint length;
char[] opIndex(uint x) { return "index"; }
char[][] opSlice(uint f,uint t) { return ["op","slice"]; }
}
void main()
{
auto x = new X;
writefln(x[length-1]); // now needs x.length
writefln(x[0..length-1]); // now needs x.length
writefln(x[$-1]);
writefln(x[0..$-1]);
with(x) {
writefln( opIndex(length-1) );
writefln( opSlice(0,length-1) );
int length = 23; // compiler does not complain :(
}
}
Note that if X were to have a member "uint first", I would be able to do
"x[first]".
Ideally, the compiler would complain if an ambiguous symbol was being
used inside with (and [], [..]).
L.
More information about the Digitalmars-d
mailing list