DMD 0.177 release [Length in slice expressions]

Chris Nicholson-Sauls ibisbasenji at gmail.com
Wed Dec 20 14:16:14 PST 2006


BCS wrote:
> Chris Nicholson-Sauls wrote:
>> The "[..$]" syntax is also present in ColdC and its relatives 
>> (including my Bovis), so it was familiar to me from the beginning.  
>> (That said I still harbor thoughts that $ could be used for other 
>> things... but honestly, I think the syntax would be unambiguous: a 
>> lone $ as the right hand side of a slice expression should easily 
>> enough be distinguishable from a $ anywhere followed by something, 
>> like an identifier.)
>>
> 
> FWIW $ is not only used for the RHS of a slice
> 
> char[] str;
> str[$/2..$];    // 2nd half of array
> str[$-1];    // last element in array
> str[$-5..$];    // last 5 things in array
> str[$-10..10];    // um... well... you get the idea

Ack.  Having never used anything quite like that before, I guess I had assumed the $ only 
had meaning as I described above.  Still, it could be possible.

>> Which leads me to another thought.  One other operator that ColdC and 
>> family posesses is the @ for list splicing.  Useless sample ColdC:
>>
>> # var foo, bar, result;
>> #
>> # foo = {1, 2, 3};
>> # bar = {4, 5, 6};
>> # result = {@foo, @bar};
>>
>> The 'result' variable now equals {1, 2, 3, 4, 5, 6}.
> 
> I would think this would be the same thing.
> 
> auto foo = [1,2,3];
> auto bar = [4,5,6];
> auto result = foo ~ bar;
> 
> am I missing somethign?

Not really, no.  But consider:

# ColdC                          D
#
# result = {@foo, 0, @bar};      result = foo ~ [0] ~ bar;
# result = {42, @someFunc()};    result = [42] ~ someFunc();
# result = {@foo, 1, @foo, 2};   result = foo ~ [1] ~ foo ~ [2];
# result = {3, 6, @myConst, 9};  result = [3, 6] ~ myConst.dup ~ [9];

It becomes part of the literal syntax, which makes things cleaner in most elaborate cases. 
  Just something I enjoy over there that I wouldn't mind seeing from time to time over 
here.  :)

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-announce mailing list