opCaret to complement opDollar when specifying slices
Steven Schveighoffer
schveiguy at yahoo.com
Wed Jun 6 04:53:42 PDT 2012
On Mon, 04 Jun 2012 17:23:57 -0400, Xinok <xinok at live.com> wrote:
> 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.
caret would have to map to 0 for slices and containers who already have a
notion of first element being zero.
This would not be hard. And in fact, would make generic code *easier*.
Right now, you have to special case containers who support slicing but do
not start on 0. The only one I know of at this point is Red Black Tree.
-Steve
More information about the Digitalmars-d
mailing list