opCaret to complement opDollar when specifying slices

Xinok xinok at live.com
Mon Jun 4 14:23:57 PDT 2012


On Saturday, 2 June 2012 at 11:49:17 UTC, Dario Schiavon wrote:
> Hi,
>
> I just read some old threads about opDollar and the wish to 
> have it work for non zero-based arrays, arrays with gaps, 
> associative arrays with non-numerical indices, and so on. It 
> was suggested to define opDollar as the end of the array rather 
> than the length (and perhaps rename opDollar to opEnd to 
> reflect this interpretation), so that collection[someIndex .. 
> $] would consistently refer to a slice from someIndex to the 
> end of the collection (of course the keys must have a defined 
> ordering for it to make sense).
>
> I'm just thinking, if we want to generalize slices for those 
> cases, shouldn't we have a symmetrical operator for the first 
> element of the array? Since the $ sign was evidently chosen to 
> parallel the regexp syntax, why don't we add ^ to refer to the 
> first element? This way, collection[^ .. $] would slice the 
> entire collection, just like collection[].
>
> Until now, ^ is only used as a binary operator, so this 
> addition shouldn't lead to ambiguous syntax. It surely wouldn't 
> be used as often as the opDollar, so I understand if you oppose 
> the idea, but it would at least make the language a little more 
> "complete".

The problem I see with this, it would be a larger burden when 
writing generic code. Libraries would have to be written to 
compensate for those containers. I'd prefer that all containers 
are simply zero-based, unless there's a need for negative indices 
(i.e. pointers). I think random-access ranges may be intended to 
be zero-based as well.


More information about the Digitalmars-d mailing list