Lets deprecate the length in the arrays index
janderson
askme at me.com
Mon Jan 15 22:50:29 PST 2007
Lionello Lunesu wrote:
> Frank Benoit (keinfarbton) wrote:
>> There was this thread:
>>
>
> 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.
Gotta say I like this idea. It would make code more manageable if you
could do things like:
char[] value = line[find('a')+1..find_r('b')];
instead of,
char[] value = line[line.find('a')+1..line.find_r('b')];
Although in something like the above you'd want to make sure that both a
and b exist before doing these searches. Playing the D advocate for a
second, maybe we don't want to encourage this style of coding?
-Joel
More information about the Digitalmars-d
mailing list