Lets deprecate the length in the arrays index

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jan 16 12:26:47 PST 2007


janderson wrote:
> 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

Even though this is just a contrived example, I still have to say that line ought to just 
expose a method for this operation, perhaps a findSlice(a,b).  If and when we get Tuples 
as return values one could also use that if you wanted to keep the find and slice 
operations seperated.

I'm just very wary of the []==with concept.  Its unintuitive, still causes the same 
namespace issues as [length], and even creates new ones.  Honestly, I've often wondered if 
something other than the dot operator should be found for built-in properties (and even 
for non built-in ones, but that goes back to my desire for an explicit property syntax, 
alas).  Sadly, I have no idea what else to use, except maybe a colon (:) but that poor 
baby is getting burdened already, and could still be ambiguous (consider `case int.max: 
...;` versus `case int:max: ...;` -- ack).

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list